Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have two arrays. The first array contains some values while the second array contains indices of the values which should be removed from the first array. For example:

var valuesArr = new Array("v1","v2","v3","v4","v5");   
var removeValFromIndex = new Array(0,2,4);

I want to remove the values present at indices 0,2,4 from valuesArr. I thought the native splice method might help so I came up with:

$.each(removeValFromIndex,function(index,value){
    valuesArr.splice(value,1);
});

But it didn't work because after each splice, the indices of the values in valuesArr were different. I could solve this problem by using a temporary array and copying all values to the second array, but I was wondering if there are any native methods to which we can pass multiple indices at which to remove values from an array.

I would prefer a jQuery solution. (Not sure if I can use grep here)

share|improve this question

8 Answers 8

up vote 7 down vote accepted

Not in-place but can be done using grep and inArray functions of jQuery.

var arr = $.grep(valuesArr, function(n, i) {
    return $.inArray(i, removeValFromIndex) ==-1;
});

alert(arr);//arr contains V2, V4

check this fiddle.

share|improve this answer
    
It would be (close enough to) in-place if you just said valuesArr = $.grep(...); –  nnnnnn Feb 24 '12 at 4:13
    
@nnnnnn ha ha ha I was leaving for a meeting (you know they make you so productive) so did not experiment much. –  TheVillageIdiot Feb 24 '12 at 7:43
    
@cept0 Why the downvote? OP asked for a jQuery solution. –  Ste77 Jun 29 '14 at 15:14

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.

share|improve this answer
2  
+1 for short sort function. –  Karna Feb 24 '12 at 4:28
    
wont slicing mess up the index –  Muhammad Umer Aug 29 '13 at 15:42
    
@MuhammadUmer - No, not if you do it correctly, which is what my answer explains. –  nnnnnn Aug 29 '13 at 19:09
3  
ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh –  Muhammad Umer Aug 29 '13 at 19:23
    
so forEach it'd be this ..var bigarr = [3,4,6,7,8,2,4,7,9] var rmv = [1,5]; rmv.forEach(function(e,i){bigarr.splice(rmv[rmv.length-i-1],1);}); –  Muhammad Umer Aug 29 '13 at 19:34

You can correct your code by replacing removeValFromIndex with removeValFromIndex.reverse(). If that array is not guaranteed to use ascending order, you can instead use removeValFromIndex.sort(function(a, b) { return b - a }).

share|improve this answer
    
Looks good to me - jsfiddle.net/mrtsherman/gDcFu/2. Although this makes the supposition that the removal list is in order. –  mrtsherman Feb 24 '12 at 3:42
    
@minopret: Thanks but it will only work if indexes in removeValFromIndex are in ascending order. –  Karna Feb 24 '12 at 3:53
function filtermethod(element, index, array) {  
    return removeValFromIndex.find(index)
}  
var result = valuesArr.filter(filtermethod);

MDN reference is here

share|improve this answer
    
@riship89 The fiddle is down –  askmatey Jun 4 '14 at 12:31
    
@Karna: fiddle is down –  riship89 Jun 4 '14 at 17:54
    
Please note that at the time of writing (june 2014), Array.prototype.find is part of the current ES6 draft and only implemented in current firefox –  Olli K Jun 14 '14 at 15:48

You could try and use delete array[index] This won't completely remove the element but rather sets the value to undefined.

share|improve this answer
    
Thanks but I want to remove the elements. –  Karna Feb 24 '12 at 3:46

Sounds like Apply could be what you are looking for.
maybe something like this would work?

Array.prototype.splice.apply(valuesArray, removeValFromIndexes );
share|improve this answer
    
But the .splice() method is not expecting a list of elements to remove, it is expecting a single index of the element at which to start removing followed by the number of elements to remove... –  nnnnnn Feb 24 '12 at 4:06

If you are using underscore.js, you can use _.filter() to solve your problem.

var valuesArr = new Array("v1","v2","v3","v4","v5"); var removeValFromIndex = new Array(0,2,4); var filteredArr = _.filter(valuesArr, function(item, index){ return !_.contains(removeValFromIndex, index); });

additionally, if you are trying to remove items using a list of items instead of indexes, you can simply use _.without(), just like:

var valuesArr = new Array("v1","v2","v3","v4","v5"); var filteredArr = _.without(valuesArr, "V1", "V3");

now filteredArr should be ["V2", "V4", "V5"]

share|improve this answer

Here's one possibility:

valuesArr = removeValFromIndex.reduceRight(function (arr, it) {
    arr.splice(it, 1);
    return arr;
}, valuesArr.sort(function (a, b) { return b - a }));

Example on jsFiddle

MDN on Array.prototype.reduceRight

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.