admin管理员组文章数量:1305166
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 traditionalfor
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
5 Answers
Reset to default 4It'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);
});
本文标签: javascriptRemove item from looped array in underscorejsStack Overflow
版权声明:本文标题:javascript - Remove item from looped array in underscorejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741798466a2398079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论