繁体   English   中英

在javascript array()中存储和检索对象;

[英]Storing and retrieving objects in javascript array();

嗨,我已经搜索了答案,但由于在线教程都是friends = ("bob","fred","joe"); 我什么都没得到。 我希望能够在数组的每个索引中存储具有1-3个值的多个对象,例如:

map = [{ground:0},{ground:0, wall:1},{ground:0, item:3}]

我现在拥有的对象看起来像:

node = {};
node = {ground:0, object:1};

但是,当我尝试无法访问数组的方式时,我得到的都是“对象对象”。 从映射数组中一对一获取值的正确方法是什么?

你的意思是:


var map = [{ground:0},{ground:0, wall:1},{ground:0, item:3}];
for(var i = 0; i < map.length; i++) {
    console.log(map[i].ground + " item " + map[i].item);
}

不确定您要的是什么,或者“数组方式”是什么意思,但是如果您想获取诸如ground的所有值,则:

var map = [{ground:0},{ground:0, wall:1},{ground:0, item:3}]

for (var i=0, iLen=map.length; i<iLen; i++) {
  alert(map[i].ground);
}

编辑

...
  alert('item ' + i + ' : ' + map[i].ground); // item 0 : 0
...

暂无
暂无

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

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