按值从数组中删除项目

时间:2022-08-26 14:06:59

I have an array of items like:

我有一系列的项目,如:

var items = [id: "animal", type: "cat", cute: "yes"]

And I'm trying to remove any items that match the ID given. In this case; animal

我正在尝试删除任何与给定ID相匹配的项目。在这种情况下;动物

I'm stuck! I can get it to work easily by having a more simpler array but this is not what I need... I also need to remove the item by value as I don't want the hassle of referring to items by their index.

我被卡住了!我可以通过使用更简单的数组来轻松地使用它,但这不是我需要的...我还需要按值删除项目,因为我不希望通过索引引用项目的麻烦。

Is there a jQuery method I could use where I don't need to iterate through the items array, rather specify a selector?

有没有我可以使用的jQuery方法,我不需要遍历items数组,而是指定一个选择器?

Here is my jsFiddle: http://jsfiddle.net/zafrX/

这是我的jsFiddle:http://jsfiddle.net/zafrX/

6 个解决方案

#1


20  

I'm not sure how much of a hassle it is to refer to array items by index. The standard way to remove array items is with the splice method

我不确定通过索引引用数组项有多麻烦。删除数组项的标准方法是使用splice方法

for (var i = 0; i < items.length; i++)
    if (items[i] === "animal") { 
        items.splice(i, 1);
        break;
    }

And of course you can generalize this into a helper function so you don't have to duplicate this everywhere.

当然,您可以将其概括为辅助函数,因此您无需在任何地方复制此函数。


EDIT

编辑

I just noticed this incorrect syntax:

我刚注意到这个错误的语法:

var items = [id: "animal", type: "cat", cute: "yes"]

Did you want something like this:

你想要这样的东西:

 var items = [ {id: "animal",  type: "cat", cute: "yes"}, {id: "mouse",  type: "rodent", cute: "no"}];

That would change the removal code to this:

这会将删除代码更改为:

for (var i = 0; i < items.length; i++)
    if (items[i].id && items[i].id === "animal") { 
        items.splice(i, 1);
        break;
    }

#2


5  

No need for jQuery or any third party lib for this, now we can use the new ES5 filter :

不需要jQuery或任何第三方lib,现在我们可以使用新的ES5过滤器:

let myArray = [{ id : 'a1', name : 'Rabbit'}, { id : 'a2', name : 'Cat'}];
myArray = myArray.filter(i => i.id !== 'a1');

#3


5  

You can either use splice or run a delete yourself. Here's an example:

您可以使用拼接或自己运行删除。这是一个例子:

for (var i = 0; i < items.length; i ++) {
    if (items[i] == "animal") { 
        items.splice(i, 1);
        break;
    }
}

#4


2  

You should do it like this (make sure that you have the right syntax...you cannot have array with properties, but object inside {} and then you can iterate by keys and delete unwanted key):

你应该这样做(确保你有正确的语法......你不能拥有属性的数组,但是{}内的对象然后你可以按键迭代并删除不需要的键):

var items = {id: "animal", type: "cat", cute: "yes"}
var removeItem = "animal"; // or with the ID matching animal...

for(var p in items){
    if(items[p] === removeItem)
        delete items[p]
}

And to answer you question, you cannot apply jquery selectors to javascript objects. The best you can do to avoid for loop is to use $.each (which is a loop written in a more "functional" way).

要回答你的问题,你不能将jquery选择器应用于javascript对象。你可以做的最好的避免for循环是使用$ .each(这是一个以更“功能”的方式编写的循环)。

#5


2  

By using object notation : http://jsfiddle.net/jrm2k6/zafrX/2/

使用对象表示法:http://jsfiddle.net/jrm2k6/zafrX/2/

var animal1 = {id: "animal", type: "cat", cute: "yes"}
var car2 = {id: "car", type: "pick-up", cute: "no"}
var animal3 = {id: "animal", type: "dog", cute: "yes"}
var removeItem = "animal"; // or with the ID matching animal...

var array_items = []
array_items.push(animal1);
array_items.push(car2);
array_items.push(animal3);

for(var i=0;i<array_items.length;i++){
    if(array_items[i].id == removeItem){
        array_items.splice(i,1);
    }
}

//alert(array_items.length);  

#6


0  

Wow, so many ideas but still not what I wanted xD

哇,这么多的想法,但仍然不是我想要的xD

This will remove ALL entries of the given value and return the removed value:

这将删除给定值的所有条目并返回删除的值:

function removeOfArray(val, arr){
    var idx;
    var ret;
    while ((idx = arr.indexOf(val)) > -1){
        arr.splice(idx, 1);
        ret = val;
    }
    return ret;
}

Also I found other solutions here: Remove item from array by value

此外,我在这里找到了其他解决方案:按值从数组中删除项目

#1


20  

I'm not sure how much of a hassle it is to refer to array items by index. The standard way to remove array items is with the splice method

我不确定通过索引引用数组项有多麻烦。删除数组项的标准方法是使用splice方法

for (var i = 0; i < items.length; i++)
    if (items[i] === "animal") { 
        items.splice(i, 1);
        break;
    }

And of course you can generalize this into a helper function so you don't have to duplicate this everywhere.

当然,您可以将其概括为辅助函数,因此您无需在任何地方复制此函数。


EDIT

编辑

I just noticed this incorrect syntax:

我刚注意到这个错误的语法:

var items = [id: "animal", type: "cat", cute: "yes"]

Did you want something like this:

你想要这样的东西:

 var items = [ {id: "animal",  type: "cat", cute: "yes"}, {id: "mouse",  type: "rodent", cute: "no"}];

That would change the removal code to this:

这会将删除代码更改为:

for (var i = 0; i < items.length; i++)
    if (items[i].id && items[i].id === "animal") { 
        items.splice(i, 1);
        break;
    }

#2


5  

No need for jQuery or any third party lib for this, now we can use the new ES5 filter :

不需要jQuery或任何第三方lib,现在我们可以使用新的ES5过滤器:

let myArray = [{ id : 'a1', name : 'Rabbit'}, { id : 'a2', name : 'Cat'}];
myArray = myArray.filter(i => i.id !== 'a1');

#3


5  

You can either use splice or run a delete yourself. Here's an example:

您可以使用拼接或自己运行删除。这是一个例子:

for (var i = 0; i < items.length; i ++) {
    if (items[i] == "animal") { 
        items.splice(i, 1);
        break;
    }
}

#4


2  

You should do it like this (make sure that you have the right syntax...you cannot have array with properties, but object inside {} and then you can iterate by keys and delete unwanted key):

你应该这样做(确保你有正确的语法......你不能拥有属性的数组,但是{}内的对象然后你可以按键迭代并删除不需要的键):

var items = {id: "animal", type: "cat", cute: "yes"}
var removeItem = "animal"; // or with the ID matching animal...

for(var p in items){
    if(items[p] === removeItem)
        delete items[p]
}

And to answer you question, you cannot apply jquery selectors to javascript objects. The best you can do to avoid for loop is to use $.each (which is a loop written in a more "functional" way).

要回答你的问题,你不能将jquery选择器应用于javascript对象。你可以做的最好的避免for循环是使用$ .each(这是一个以更“功能”的方式编写的循环)。

#5


2  

By using object notation : http://jsfiddle.net/jrm2k6/zafrX/2/

使用对象表示法:http://jsfiddle.net/jrm2k6/zafrX/2/

var animal1 = {id: "animal", type: "cat", cute: "yes"}
var car2 = {id: "car", type: "pick-up", cute: "no"}
var animal3 = {id: "animal", type: "dog", cute: "yes"}
var removeItem = "animal"; // or with the ID matching animal...

var array_items = []
array_items.push(animal1);
array_items.push(car2);
array_items.push(animal3);

for(var i=0;i<array_items.length;i++){
    if(array_items[i].id == removeItem){
        array_items.splice(i,1);
    }
}

//alert(array_items.length);  

#6


0  

Wow, so many ideas but still not what I wanted xD

哇,这么多的想法,但仍然不是我想要的xD

This will remove ALL entries of the given value and return the removed value:

这将删除给定值的所有条目并返回删除的值:

function removeOfArray(val, arr){
    var idx;
    var ret;
    while ((idx = arr.indexOf(val)) > -1){
        arr.splice(idx, 1);
        ret = val;
    }
    return ret;
}

Also I found other solutions here: Remove item from array by value

此外,我在这里找到了其他解决方案:按值从数组中删除项目