Remove Multiple Indices from Array

Remove multiple elements from array in Javascript/jQuery

There's always the plain old for loop:

var valuesArr = ["v1","v2","v3","v4","v5"],
removeValFromIndex = [0,2,4];

for (var i = removeValFromIndex.length -1; i >= 0; i--)
valuesArr.splice(removeValFromIndex[i],1);

Go through removeValFromIndex in reverse order and you can .splice() without messing up the indexes of the yet-to-be-removed items.

Note in the above I've used the array-literal syntax with square brackets to declare the two arrays. This is the recommended syntax because new Array() use is potentially confusing given that it responds differently depending on how many parameters you pass in.

EDIT: Just saw your comment on another answer about the array of indexes not necessarily being in any particular order. If that's the case just sort it into descending order before you start:

removeValFromIndex.sort(function(a,b){ return b - a; });

And follow that with whatever looping / $.each() / etc. method you like.

How do I remove multiple elements from an array by index?

There's a Lodash method _.pullAt that pulls elements out of the array (in place) that are specified in an array:

_.pullAt(array, [indexes])

Removes elements from array corresponding to indexes and returns an array of removed elements.

Thus, apply it like this:

var array = ["Apple", "Banana", "Peach", "Pumpkin", "Tomato", "Mango", "Guava"];
_.pullAt(array, [4, 5]);

And if you want to pull out non-adjacent items (from the documentation):

var array = ['a', 'b', 'c', 'd'];
var pulled = _.pullAt(array, [1, 3]);

console.log(array);
// => ['a', 'c']

This will remove the elements at index 4 and 5 in place, and also return the removed element (if you need it).


You could, of course with vanilla JavaScript, look backwards as Nick A. mentioned and remove items from the back. The reason this works is because removing from the back will not make a changing length an issue. Consider this example where I want to remove the 1st and 3rd elements:

[1, 3, 7, 9]

If I were to loop 'forwards', I would remove element at index 1 (3), but the length would change to 3 and there would be no element at index 3! If I were to loop backwards though, I would remove the element at index 3 first (9). Then the length would be 3, but that doesn't affect any elements before it, the element at index 1 is still at index 1 thus removing from the back works.

How do I remove multiple elements from an array?

The issue is with your indexing, you are finding the element using the index of arr, and deleting in the array, which is probably causing issue with indexing in loop.
Modify your code as follows

const removeFromArray = function (arr, ...theArgs) {
for (let i = 0; i < theArgs.length; i++) {
if (arr.includes(theArgs[i])) {
arr.splice(arr.indexOf(theArgs[i]), 1);
}
}
return arr;
};

The above fixes the code your way, but a better way of doing it would be using filter.

const removeFromArray = function (arr, ...theArgs) {
return arr.filter(ele => !theArgs.includes(ele))
}

I am writing it this way to purely maintain your function.

Remove multiple indices from array

Rather than a list of indices to remove, it may be easier to have a list of indices to keep, which you can do using the Set type:

let rmIndices = [1,4,5]
let keepIndices = Set(arr.indices).subtract([1,4,5])

Then you can use PermutationGenerator to create a fresh array of just those indices:

arr = Array(PermutationGenerator(elements: arr, indices: keepIndices))

How to remove more than one elements from an array from different indexes at same time in JavaScript?

You can use trailing comma at destructuring assignment to select specific elements from array, assign resulting values within array to original array reference.