简体   繁体   English

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

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

Hi I've searched for the answer but since the tutorials online are all friends = ("bob","fred","joe"); 嗨,我已经搜索了答案,但由于在线教程都是friends = ("bob","fred","joe"); I dont get anywhere. 我什么都没得到。 I would like to be able to store several objects with 1-3 values in each index of the array like: 我希望能够在数组的每个索引中存储具有1-3个值的多个对象,例如:

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

The object I have right now looks like: 我现在拥有的对象看起来像:

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

But when I try the array way I can't access it, all I get "object Object". 但是,当我尝试无法访问数组的方式时,我得到的都是“对象对象”。 What would be the correct way to get the values from the map array one by one? 从映射数组中一对一获取值的正确方法是什么?

Do you mean: 你的意思是:


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

Not sure what you want, or what you mean by "the array way", but if you want to get all the values for say ground , then: 不确定您要的是什么,或者“数组方式”是什么意思,但是如果您想获取诸如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);
}

Edit 编辑

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

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

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