使用Underscore.js从数组中删除Item [英] Removing Item from array with Underscore.js
本文介绍了使用Underscore.js从数组中删除Item的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的数组:
var array = [1,20,50,60,78,90];
var id = 50;
如何从数组中删除id并返回一个没有值的新数组新数组中的id?
How can i remove the id from the array and return a new array that does not have the value of the id in new array?
推荐答案
对于复杂的解决方案,您可以使用方法 _。reject()
,这样就可以将自定义逻辑放入回调中:
For the complex solutions you can use method _.reject()
, so that you can put a custom logic into callback:
var removeValue = function(array, id) {
return _.reject(array, function(item) {
return item === id; // or some complex logic
});
};
var array = [1, 20, 50, 60, 78, 90];
var id = 50;
console.log(removeValue(array, id));
对于简单的情况,请使用更方便的方法 _。不带()
:
For the simple cases use more convenient method _.without()
:
var array = [1, 20, 50, 60, 78, 90];
var id = 50;
console.log(_.without(array, id));
这篇关于使用Underscore.js从数组中删除Item的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文