最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove item from looped array in underscorejs - Stack Overflow

programmeradmin0浏览0评论

I have an array like this

var array = [1,2,3,4,5,6,7,8,9,10];

Looping code is like this using _.each function in underscore.js

_.each(array,function(item,index){
  console.log(item);
});

But I want remove some items in array when looping. For example I need to remove number 5 from array and loop does not print number 5. The question is, is it possible to remove items in array when looping on this array?

I have an array like this

var array = [1,2,3,4,5,6,7,8,9,10];

Looping code is like this using _.each function in underscore.js

_.each(array,function(item,index){
  console.log(item);
});

But I want remove some items in array when looping. For example I need to remove number 5 from array and loop does not print number 5. The question is, is it possible to remove items in array when looping on this array?

Share Improve this question edited Mar 11, 2019 at 14:15 T J 43.2k13 gold badges86 silver badges142 bronze badges asked Oct 12, 2013 at 11:09 MBehtemamMBehtemam 7,91917 gold badges68 silver badges111 bronze badges 2
  • 1 I'm not familiar with underscore methods, but in a general sense it's not a good idea to remove array items when using an iterator function like .each() because (again I'm not sure about underscore, but for some other equivalents) the length of the array may be cached at the beginning so then if you remove items the iterator will run off the end. It's no problem if iterating with a traditional for loop. – nnnnnn Commented Oct 12, 2013 at 11:11
  • @nnnnnn: It is if he forgets i-- whenever he removes the current element ;) – ThiefMaster Commented Oct 12, 2013 at 11:11
Add a ment  | 

5 Answers 5

Reset to default 4

It's usually a very bad idea to modify an array while iterating over it. The best solution is to store the indexes in a separate array and remove them afterwards (remember to iterate over that array from last to first so you don't have to deal with changing indexes).

2 ways, I'd remend the first one.

var array = [1,2,3,4,5,6,7,8],
    items_to_remove = [], i;

_.each(array, function(item,index){
    if(item === 'something'){
        items_to_remove.push(index);
    }
});

while((i = items_to_remove.pop()) != null){
    array.splice(i, 1);
}

// OR
for(i = array.length - 1; i > -1; --i) {
    if(array[i] === 'something') {
        array.splice(i, 1);
    }
}

With underscore you could do this:

var array = [1,2,3,4,5,6,7,8,9,10];

var filteredList = _.filter(array, function(item){

    // do something with each item - here just log the item
    console.log(item);

    // only want even numbers
    return item % 2 == 0;

});

// show filtered list
_.each(filteredList, function(item){
    console.log(item);
});

You can use Undersocre to reduce the line of code. Underscore is an handy-tool.

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array = _.without(array, _.findWhere(array, {
  id: 3
}));
console.log(arr);

Here's a simple inline solution that worked for me using underscore's reject function:

_.each(_.reject(array, function(arrayItem) { return arrayItem === 5}), function(item){
  console.log(item);
});
发布评论

评论列表(0)

  1. 暂无评论