使用UnderscoreJS从数组中删除项目

时间:2022-08-26 14:24:17

Say I have this code

说我有这个代码

var arr = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}];

and I want to remove the item with id = 3 from the array. Is there a way of doing this without splicing? Maye something using underscore or something like that?

Thanks!

我想从数组中删除id = 3的项目。有没有一种方法可以做到这一点没有拼接? Maye使用下划线或类似的东西?谢谢!

9 个解决方案

#1


250  

Just using plain JavaScript, this has been answered already: remove objects from array by object property.

只使用普通的JavaScript,这已经得到了回答:通过对象属性从数组中删除对象。

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

使用underscore.js,您可以将.findWhere与.without结合使用:

var arr = [{
  id: 1,
  name: 'a'
}, {
  id: 2,
  name: 'b'
}, {
  id: 3,
  name: 'c'
}];

//substract third
arr = _.without(arr, _.findWhere(arr, {
  id: 3
}));
console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

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.

虽然,既然你在这种情况下创建了一个新的数组,你可以简单地使用_.filter或原生的Array.prototype.filter函数(就像在另一个问题中所示)。那么你只会迭代一次数组而不是像这里一样两次。

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.

如果要在适当位置修改阵列,则必须使用.splice。这也在另一个问题中显示,并且非核心似乎没有为此提供任何有用的功能。

#2


91  

You can use Underscore .filter

你可以使用Underscore .filter

    var arr = [{
      id: 1,
      name: 'a'
    }, {
      id: 2,
      name: 'b'
    }, {
      id: 3,
      name: 'c'
    }];

    var filtered = _(arr).filter(function(item) {
         return item.id !== 3
    });

Can also be written as:

也可以写成:

var filtered = arr.filter(function(item) {
    return item.id !== 3
});

var filtered = _.filter(arr, function(item) {
    return item.id !== 3
});

Check Fiddle

检查小提琴

You can also use .reject

您也可以使用.reject

#3


34  

Use Underscore _.reject():

使用Underscore _.reject():

arr = _.reject(arr, function(d){ return d.id === 3; });

#4


14  

Underscore has a _without() method perfect for removing an item from an array, especially if you have the object to remove.

Underscore有一个_without()方法,非常适合从数组中删除项目,特别是如果你有要删除的对象。

Returns a copy of the array with all instances of the values removed.

返回数组的副本,其中删除了所有值的实例。

_.without(["bob", "sam", "fred"], "sam");

=> ["bob", "fred"]

Works with more complex objects too.

也适用于更复杂的对象。

var bob = { Name: "Bob", Age: 35 };
var sam = { Name: "Sam", Age: 19 };
var fred = { Name: "Fred", Age: 50 };

var people = [bob, sam, fred]

_.without(people, sam);

=> [{ Name: "Bob", Age: 35 }, { Name: "Fred", Age: 50 }];

If you don't have the item to remove, just a property of it, you can use _.findWhere and then _.without.

如果您没有要删除的项目,只有它的属性,您可以使用_.findWhere然后_.without。

#5


5  

Please exercise care if you are filtering strings and looking for case insensitive filters. _.without() is case sensitive. You can also use _.reject() as shown below.

如果您正在过滤字符串并查找不区分大小写的过滤器,请小心操作。 _.without()区分大小写。您也可以使用_.reject(),如下所示。

var arr = ["test","test1","test2"];

var filtered = _.filter(arr, function(arrItem) {
    return arrItem.toLowerCase() !== "TEST".toLowerCase();
});
console.log(filtered);
// ["test1", "test2"]

var filtered1 = _.without(arr,"TEST");
console.log(filtered1);
// ["test", "test1", "test2"]

var filtered2 = _.reject(arr, function(arrItem){ 
    return arrItem.toLowerCase() === "TEST".toLowerCase();
});
console.log(filtered2);
// ["test1", "test2"]

#6


2  

or another handy way:

或另一种方便的方式:

_.omit(arr, _.findWhere(arr, {id: 3}));

my 2 cents

我的2美分

#7


2  

arr.splice(_.findIndex(arr, { id: 3 }), 1);

#8


0  

I used to try this method

我曾经尝试过这种方法

_.filter(data, function(d) { return d.name != 'a' });

There might be better methods too like the above solutions provided by users

可能有更好的方法,就像用户提供的上述解决方案一样

#9


-1  

You can use reject method of Underscore, below will return a new array which won't have array with particular match

您可以使用Underscore的reject方法,下面将返回一个新数组,该数组不具有特定匹配的数组

arr = _.reject(arr, function(objArr){ return objArr.id == 3; });

#1


250  

Just using plain JavaScript, this has been answered already: remove objects from array by object property.

只使用普通的JavaScript,这已经得到了回答:通过对象属性从数组中删除对象。

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

使用underscore.js,您可以将.findWhere与.without结合使用:

var arr = [{
  id: 1,
  name: 'a'
}, {
  id: 2,
  name: 'b'
}, {
  id: 3,
  name: 'c'
}];

//substract third
arr = _.without(arr, _.findWhere(arr, {
  id: 3
}));
console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

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.

虽然,既然你在这种情况下创建了一个新的数组,你可以简单地使用_.filter或原生的Array.prototype.filter函数(就像在另一个问题中所示)。那么你只会迭代一次数组而不是像这里一样两次。

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.

如果要在适当位置修改阵列,则必须使用.splice。这也在另一个问题中显示,并且非核心似乎没有为此提供任何有用的功能。

#2


91  

You can use Underscore .filter

你可以使用Underscore .filter

    var arr = [{
      id: 1,
      name: 'a'
    }, {
      id: 2,
      name: 'b'
    }, {
      id: 3,
      name: 'c'
    }];

    var filtered = _(arr).filter(function(item) {
         return item.id !== 3
    });

Can also be written as:

也可以写成:

var filtered = arr.filter(function(item) {
    return item.id !== 3
});

var filtered = _.filter(arr, function(item) {
    return item.id !== 3
});

Check Fiddle

检查小提琴

You can also use .reject

您也可以使用.reject

#3


34  

Use Underscore _.reject():

使用Underscore _.reject():

arr = _.reject(arr, function(d){ return d.id === 3; });

#4


14  

Underscore has a _without() method perfect for removing an item from an array, especially if you have the object to remove.

Underscore有一个_without()方法,非常适合从数组中删除项目,特别是如果你有要删除的对象。

Returns a copy of the array with all instances of the values removed.

返回数组的副本,其中删除了所有值的实例。

_.without(["bob", "sam", "fred"], "sam");

=> ["bob", "fred"]

Works with more complex objects too.

也适用于更复杂的对象。

var bob = { Name: "Bob", Age: 35 };
var sam = { Name: "Sam", Age: 19 };
var fred = { Name: "Fred", Age: 50 };

var people = [bob, sam, fred]

_.without(people, sam);

=> [{ Name: "Bob", Age: 35 }, { Name: "Fred", Age: 50 }];

If you don't have the item to remove, just a property of it, you can use _.findWhere and then _.without.

如果您没有要删除的项目,只有它的属性,您可以使用_.findWhere然后_.without。

#5


5  

Please exercise care if you are filtering strings and looking for case insensitive filters. _.without() is case sensitive. You can also use _.reject() as shown below.

如果您正在过滤字符串并查找不区分大小写的过滤器,请小心操作。 _.without()区分大小写。您也可以使用_.reject(),如下所示。

var arr = ["test","test1","test2"];

var filtered = _.filter(arr, function(arrItem) {
    return arrItem.toLowerCase() !== "TEST".toLowerCase();
});
console.log(filtered);
// ["test1", "test2"]

var filtered1 = _.without(arr,"TEST");
console.log(filtered1);
// ["test", "test1", "test2"]

var filtered2 = _.reject(arr, function(arrItem){ 
    return arrItem.toLowerCase() === "TEST".toLowerCase();
});
console.log(filtered2);
// ["test1", "test2"]

#6


2  

or another handy way:

或另一种方便的方式:

_.omit(arr, _.findWhere(arr, {id: 3}));

my 2 cents

我的2美分

#7


2  

arr.splice(_.findIndex(arr, { id: 3 }), 1);

#8


0  

I used to try this method

我曾经尝试过这种方法

_.filter(data, function(d) { return d.name != 'a' });

There might be better methods too like the above solutions provided by users

可能有更好的方法,就像用户提供的上述解决方案一样

#9


-1  

You can use reject method of Underscore, below will return a new array which won't have array with particular match

您可以使用Underscore的reject方法,下面将返回一个新数组,该数组不具有特定匹配的数组

arr = _.reject(arr, function(objArr){ return objArr.id == 3; });