Javascript数组引用

时间:2008-09-17 19:43:17

标签: javascript json

如果我有以下内容:

{"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"}
   {"Make":"Toyota","Model":"Corolla","Year":"2008"}
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
}

我有一个“hdrs”名称(即“Make”),如何引用data数组实例? 似乎data["Make"][0]应该有效...但无法获得正确的参考

编辑

很抱歉有歧义..我可以遍历hdrs来获取每个hdr名称,但我需要使用hdrs的每个实例值来查找data中的所有数据元素(不确定是否有更好的解释)。我将在变量t中使用它,因为它是JSON(感谢重新标记)我希望能够引用这样的内容:t.data[hdrs[i]][j]

9 个答案:

答案 0 :(得分:3)

我不得不改变你的代码:

var x = {"hdrs": ["Make","Model","Year"],
         "data" : [ 
           {"Make":"Honda","Model":"Accord","Year":"2008"},
           {"Make":"Toyota","Model":"Corolla","Year":"2008"},
           {"Make":"Honda","Model":"Pilot","Year":"2008"}]
        };

        alert( x.data[0].Make );

编辑:回复您的编辑

var x = {"hdrs": ["Make","Model","Year"],
         "data" : [ 
           {"Make":"Honda","Model":"Accord","Year":"2008"},
           {"Make":"Toyota","Model":"Corolla","Year":"2008"},
           {"Make":"Honda","Model":"Pilot","Year":"2008"}]
        };
var Header = 0; // Make
for( var i = 0; i <= x.data.length - 1; i++ )
{
    alert( x.data[i][x.hdrs[Header]] );
}           

答案 1 :(得分:1)

那么,像这样?

var theMap = /* the stuff you posted */;
var someHdr = "Make";
var whichIndex = 0;
var correspondingData = theMap["data"][whichIndex][someHdr];

如果我理解正确的话,这应该有用......

答案 2 :(得分:1)

var x = {"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"}
   {"Make":"Toyota","Model":"Corolla","Year":"2008"}
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
};

x.data[0].Make == "Honda"
x['data'][0]['Make']  == "Honda"

你有向后的数组/哈希查找:)

答案 3 :(得分:1)

首先,您忘记了数据数组项中的尾随逗号。

尝试以下方法:

var obj_hash = {
    "hdrs": ["Make","Model","Year"],
    "data" : [
        {"Make":"Honda","Model":"Accord","Year":"2008"},
        {"Make":"Toyota","Model":"Corolla","Year":"2008"},
        {"Make":"Honda","Model":"Pilot","Year":"2008"},
    ]
};

var ref_data = obj_hash.data;

alert(ref_data[0].Make);

@ Kent Fredric:请注意,最后一个逗号不是严格需要的,但允许您更轻松地移动线条(即,如果您在最后一行之后移动或添加它,它没有逗号,你必须特别记得添加一个逗号。我认为最好总是留下逗号。)

答案 4 :(得分:1)

我不确定我理解你的问题,但是......

假设上面的JSON是var obj,你想要:

obj.data[0]["Make"] // == "Honda"

如果您只想引用第一个标题引用的字段,它将类似于:

obj.data[0][obj.hdrs[0]] // == "Honda"

答案 5 :(得分:0)

也许尝试数据[0]。制作

答案 6 :(得分:0)

关闭,你要用

var x = data[0].Make;
var z = data[0].Model;
var y = data[0].Year;

答案 7 :(得分:0)

显示的代码在语法上不正确;它需要一些逗号。我得到了这个工作:

$foo = {"hdrs": ["Make","Model","Year"],
 "data" : [ 
   {"Make":"Honda","Model":"Accord","Year":"2008"},
   {"Make":"Toyota","Model":"Corolla","Year":"2008"},
   {"Make":"Honda","Model":"Pilot","Year":"2008"}]
};

然后我可以访问数据:

$foo["data"][0]["make"]

答案 8 :(得分:0)

在答案的帮助下(以及在内部和外部循环正确之后)我得到了这个工作:

var t = eval( "(" + request + ")" ) ;
for (var i = 0; i < t.data.length; i++) {
 myTable +=    "<tr>";
 for (var j = 0; j < t.hdrs.length; j++) {
  myTable += "<td>" ;
   if (t.data[i][t.hdrs[j]] == "") {myTable += "&nbsp;" ; }
    else { myTable += t.data[i][t.hdrs[j]] ; }
  myTable += "</td>";
 }
 myTable +=    "</tr>";
}