Javascript 从多维数组中删除元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10174750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:10:27  来源:igfitidea点击:

Remove element from multidimensional array

javascriptobject

提问by Milos911

I have following multidimensional array:

我有以下多维数组:

{"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}}

In this example i want to remove

在这个例子中,我想删除

"1":{"cid":"1","uid":"3"}

from it. I have tried all solutions that i found on stackoverflow, and could not make any of them work. I am mostly php person, so i might miss something important here?

从中。我已经尝试了我在 stackoverflow 上找到的所有解决方案,但无法使它们中的任何一个工作。我主要是 php 人,所以我可能会错过一些重要的东西?

回答by Sirko

Just use deletewith the appropriate property.

只需使用delete适当的属性。

var obj = {"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}};

delete obj["1"];

Note the "around the 1 to mark it as an identifier and not as an array index!

注意"1 周围的将其标记为标识符而不是数组索引!

EDITAs pointed out in the comment, objis an object and no array no matter how you address the [1]property. My last note was just to make it clear that you are working with an object and not an array.

编辑正如评论中所指出的,obj无论您如何处理该[1]属性,它都是一个对象而不是数组 。我的最后一个说明只是为了明确您正在使用一个对象而不是一个数组。

In my experience most people associate integer properties with arrays and string properties with objects. So I thought it might be more helpful to highlight the property in the way given.

根据我的经验,大多数人将整数属性与数组相关联,将字符串属性与对象相关联。所以我认为以给定的方式突出显示属性可能更有帮助。

回答by Royi Namir

 var myObj= {"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}}

delete myObj['1'];

alert ( myObj['1']);

please notice there are Cross platform problems with delete :

请注意删除存在跨平台问题:

Cross-browser issues

跨浏览器问题

Although ECMAScript makes iteration order of objects implementation-dependent, it may appear that all major browsers support an iteration order based on the earliest added property coming first (at least for properties not on the prototype). However, in the case of Internet Explorer, when one uses delete on a property, some confusing behavior results, preventing other browsers from using simple objects like object literals as ordered associative arrays. In Explorer, while the property value is indeed set to undefined, if one later adds back a property with the same name, the property will be iterated in its old position--not at the end of the iteration sequence as one might expect after having deleted the property and then added it back.

尽管 ECMAScript 使对象的迭代顺序依赖于实现,但似乎所有主要浏览器都支持基于最早添加的属性的迭代顺序(至少对于不在原型上的属性)。但是,在 Internet Explorer 的情况下,当对属性使用 delete 时,会产生一些令人困惑的行为,从而阻止其他浏览器使用简单对象(如对象文字)作为有序关联数组。在资源管理器中,虽然属性值确实设置为未定义,但如果稍后添加回具有相同名称的属性,则该属性将在其旧位置进行迭代——而不是像人们所期望的那样在迭代序列的末尾删除了该属性,然后将其添加回来。

回答by Rakesh

var arr=[[10,20,30],[40,50,60],[70,80,90]];

for(var i=0;i<arr.length;i++){
    for(var j=0;j<arr[i].length;j++){
        if(arr[i][j]==50){
            arr[i].splice(j,1);
        }
    }
}

document.write(arr);

回答by Vimalnath

You could use delete in javascript Ex:

您可以在 javascript 中使用 delete 示例:

var x = {"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}};

delete x['1'];

Also, check this:

另外,请检查:

Deleting Objects in JavaScript

在 JavaScript 中删除对象

回答by Elias Van Ootegem

JavaScript doesn't have multi dimensional Arrays, only arrays and objects. That said: delete theObject['1'];should work just fine

JavaScript 没有多维数组,只有数组和对象。那说:delete theObject['1'];应该工作得很好

回答by Kappei

Assign your Object(not an Array) to a variable, this way:

将您的Object(不是Array)分配给变量,如下所示:

var o = {"2":{"cid":"2","uid":"2"},"1":{"cid":"1","uid":"3"}};

Then do:

然后做:

delete o['1'];

That's it!

就是这样!