从关联数组中删除值[重复]

时间:2022-06-12 03:55:35

Possible Duplicate:
Remove specific element from a javascript array?

可能重复:从javascript数组中删除特定元素?

I am having an array, from which I want to remove a value.

我有一个数组,我想从中删除一个值。

Consider this as an array

将此视为一个数组

[ 'utils': [ 'util1', 'util2' ] ]

['utils':['util1','util2']]

Now I just want to remove util2. How can I do that, I tried using delete but it didn't work.

现在我只想删除util2。我怎么能这样做,我尝试使用删除但它没有用。

Can anyone help me?

谁能帮我?

3 个解决方案

#1


5  

Use the splice method:

使用拼接方法:

var object = { 'utils': [ 'util1', 'util2' ] }

object.utils.splice(1, 1);

If you don't know the actual position of the array element, you'd need to iterate over the array and splice the element from there. Try the following method:

如果您不知道数组元素的实际位置,则需要迭代数组并从那里拼接元素。尝试以下方法:

for (var i = object.utils.length; i--;) {
    var index = object.utils.indexOf('util2');

    if (index === -1) break;

    if (i === index) {
        object.utils.splice(i, 1); break;
    }
}

Update: techfoobar's answer seems to be more idiomatic than mine. Consider using his instead.

更新:techfoobar的答案似乎比我的更为惯用。考虑使用他而不是。

#2


5  

You can use Array.splice() in combo with Array.indexOf() to get the desired behavior without having to loop through the array:

您可以在Array.indexOf()的组合中使用Array.splice()来获得所需的行为,而无需遍历数组:

var toDelete = object.utils.indexOf('util1');
if(toDelete != -1) {
    object.utils.splice(toDelete, 1);
}

#3


0  

I think there is no such thing called associative array in Javascript. It is actually an object.

我认为在Javascript中没有称为关联数组的东西。它实际上是一个对象。

[ 'utils': [ 'util1', 'util2' ] ]

For this line, I don't think it would compile. You should write this instead.

对于这一行,我认为它不会编译。你应该写这个。

var obj = { 'utils': [ 'util1', 'util2' ] }

So to remove the element "util2" in the array (the inside array), there are 2 ways.

因此,要删除数组中的元素“util2”(内部数组),有两种方法。

  1. use pop()

    使用pop()

    obj["utils"].pop(); // obj["utils"] is used to access the "property" of the object, not an element in associative array
    
  2. reduce the length

    缩短长度

    obj["utils"].length --;
    

#1


5  

Use the splice method:

使用拼接方法:

var object = { 'utils': [ 'util1', 'util2' ] }

object.utils.splice(1, 1);

If you don't know the actual position of the array element, you'd need to iterate over the array and splice the element from there. Try the following method:

如果您不知道数组元素的实际位置,则需要迭代数组并从那里拼接元素。尝试以下方法:

for (var i = object.utils.length; i--;) {
    var index = object.utils.indexOf('util2');

    if (index === -1) break;

    if (i === index) {
        object.utils.splice(i, 1); break;
    }
}

Update: techfoobar's answer seems to be more idiomatic than mine. Consider using his instead.

更新:techfoobar的答案似乎比我的更为惯用。考虑使用他而不是。

#2


5  

You can use Array.splice() in combo with Array.indexOf() to get the desired behavior without having to loop through the array:

您可以在Array.indexOf()的组合中使用Array.splice()来获得所需的行为,而无需遍历数组:

var toDelete = object.utils.indexOf('util1');
if(toDelete != -1) {
    object.utils.splice(toDelete, 1);
}

#3


0  

I think there is no such thing called associative array in Javascript. It is actually an object.

我认为在Javascript中没有称为关联数组的东西。它实际上是一个对象。

[ 'utils': [ 'util1', 'util2' ] ]

For this line, I don't think it would compile. You should write this instead.

对于这一行,我认为它不会编译。你应该写这个。

var obj = { 'utils': [ 'util1', 'util2' ] }

So to remove the element "util2" in the array (the inside array), there are 2 ways.

因此,要删除数组中的元素“util2”(内部数组),有两种方法。

  1. use pop()

    使用pop()

    obj["utils"].pop(); // obj["utils"] is used to access the "property" of the object, not an element in associative array
    
  2. reduce the length

    缩短长度

    obj["utils"].length --;