简体   繁体   English

从javascript数组内的对象中删除/添加特定变量?

[英]Removing/Adding a specific variable from an object inside javascript array?

I have a map array with objects stuffed with variables looking like this: 我有一个地图数组,其中的对象填充有如下所示的变量:

var map = [
[{ground:0, object:1}, {ground:0, item:2}, {ground:0, object:1, item:2}],
[{ground:0, object:1}, {ground:0, item:2}, {ground:0, object:1, item:2}]
];

Now I would like to be able to delete and add one of the variables like item:2 . 现在,我希望能够删除和添加像item:2这样的变量之一。

1) What would I use to delete specific variables? 1)我将使用什么删除特定变量?

2) What would I use to add specific variables? 2)我将使用什么来添加特定变量?

I just need 2 short lines of code, the rest like detecting if and where to execute I've figured out. 我只需要两行短代码,其余的就像检测出我确定的是否执行以及在哪里执行。

I've tried delete map[i][j].item; 我试过delete map[i][j].item; with no results. 没有结果。

Help appreciated. 帮助表示赞赏。

delete map[i][j].item should be what you need. delete map[i][j].item应该是您所需要的。 Here's my test run in the Javascript console (Chrome) 这是我在Javascript控制台(Chrome)中运行的测试

> var map = [
    [{ground:0, object:1}, {ground:0, item:2}, {ground:0, object:1, item:2}],
    [{ground:0, object:1}, {ground:0, item:2}, {ground:0, object:1, item:2}]
  ];
  undefined

> map[0][1]
  Object
    ground: 0
    item: 2
    __proto__: Object

> delete map[0][1].item
  true

> map[0][1]
  Object
    ground: 0
    __proto__: Object

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

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