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
add comment

6 Answers

up vote 4 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
add comment

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
1  
+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
 
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
show 2 more comments
function filtermethod(element, index, array) {  
    return removeValFromIndex.find(index)
}  
var result = valuesArr.filter(filtermethod);

MDN reference is here

working fiddle is here

share|improve this answer
 
jsfiddle.net/Ajinkya_Parakh/gDcFu/3 –  Karna Feb 24 '12 at 3:56
add comment

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
add comment

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
add comment

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
add comment

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.