簡體   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