简体   繁体   English

从多维数组中删除元素

[英]Remove element from multidimensional array

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. 我已经尝试了我在stackoverflow上找到的所有解决方案,并且无法使它们中的任何一个工作。 I am mostly php person, so i might miss something important here? 我主要是php的人,所以我可能会错过一些重要的东西吗?

Just use delete with 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左右将其标记为标识符而不是数组索引!

EDIT As pointed out in the comment, obj is an object and no array no matter how you address the [1] property. 编辑正如评论中所指出的,无论你如何处理[1]属性, obj都是一个对象,没有数组。 My last note was just to make it clear that you are working with an object and not an array. 我的最后一点是为了清楚地表明你正在使用一个对象而不是一个数组。

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. 所以我认为以给定的方式强调财产可能更有帮助。

 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). 虽然ECMAScript使对象的迭代顺序依赖于实现,但似乎所有主流浏览器都支持基于最先添加的属性的迭代顺序(至少对于不在原型上的属性)。 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. 但是,对于Internet Explorer,当在属性上使用delete时,会导致一些令人困惑的行为,从而阻止其他浏览器将对象文字等简单对象用作有序关联数组。 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. 在资源管理器中,虽然属性值确实设置为未定义,但如果后来添加了一个具有相同名称的属性,则该属性将在其旧位置迭代 - 而不是在迭代序列的末尾,正如人们可能期望的那样删除了该属性,然后将其添加回来。

JavaScript doesn't have multi dimensional Arrays, only arrays and objects. JavaScript没有多维数组,只有数组和对象。 That said: delete theObject['1']; 那说: delete theObject['1']; should work just fine 应该工作得很好

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);

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! 而已!

You could use delete in javascript Ex: 您可以在javascript Ex中使用delete:

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

delete x['1'];

Also, check this: 另外,检查一下:

Deleting Objects in JavaScript 在JavaScript中删除对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM