使用下划线从数组中删除项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用下划线从数组中删除项相关的知识,希望对你有一定的参考价值。

Using underscore.js, you could combine .findWhere with .without

Although, since you are creating a new array in this case anyway, you could simply use _.filter or the native Array.prototype.filter function (just like shown in the other question). Then you would only iterate over array once instead of potentially twice like here.

If you want to modify the array in-place, you have to use .splice. This is also shown in the other question and undescore doesn't seem to provide any useful function for that.
  1. var arr = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}];
  2.  
  3. arr = _.without(arr, _.findWhere(arr, {id: 3}));

以上是关于使用下划线从数组中删除项的主要内容,如果未能解决你的问题,请参考以下文章

使用 std::map 从数组中删除重复项

从排序数组中删除重复项(不同的最终结果)

从排序数组中删除重复项,不同代码方式的相同解决方案具有不同的输出

如何从数组中删除随机项,然后将其从数组中删除,直到数组为空

从数组中删除所有某些重复项[重复]

从排序数组中删除重复项 Leetcode