简体   繁体   English

如何访问所有嵌套的关联数组元素?

[英]How to access all nested associative array elements?

Situation : I receive JSON array from jQuery <-> PHP Ajax request. 情况:我从jQuery <-> PHP Ajax请求接收JSON数组。 Here's structure of unparsed JSON aray : 这是未解析的JSON aray的结构:

{"Focus":{"id":2,"brand":"Ford","name":"Focus"}}

And after using JSON.parse(json); 然后使用JSON.parse(json); the structure looks like : 结构看起来像:

    Focus: Object
       brand: "Ford"
       id: 2
       name: "Focus"

Problem : I want to access all array's '1st tier' elements and use them like an object, but none of the following ways works : 问题:我想访问所有数组的“第一层”元素并将它们像一个对象一样使用,但是以下任何一种方法均无效:

for (var entity in dataTable)
{
   formattedText += entity['brand'] + " " + entity['name'] + "<br>";
   OR
   formattedText += entity.brand + " " + entity.name + "<br>";
   OR
   formattedText += dataTable[0]['brand'] + " " + dataTable[0]['name'] + "<br>";
}

Any solutions how to read values of all object in this array? 任何解决方案如何读取此数组中所有对象的值?

The for..in loop uses keys and does not return the elements themself: for (var key in dataTable) for..in循环使用 ,而不自己返回元素: for (var key in dataTable)
You would then access each element with dataTable[key] . 然后,您将使用dataTable[key]访问每个元素。 The key is actually the name of the Element. 关键实际上是元素的名称。

You where using it as you would use a for..of loop, but that is a new feature not supported in all Browsers yet. 您可以像在for..of循环中那样使用它,但这是所有浏览器尚不支持的新功能。

Demo: 演示:

 var dataTable = {"Focus":{"id":2,"brand":"Ford","name":"Focus"}} var formattedText = "" for (var key in dataTable) { formattedText += dataTable[key]['brand'] + " " + dataTable[key]['name'] + "<br>"; } document.write(formattedText) 

Object.keys will return array of all the keys of the object Object.keys将返回object的所有键的数组

You can loop( forEach / for-loop ) through the keys to get the expected output. 您可以通过keys循环( forEach / for-loop )以获得预期的输出。

Using forEach : 使用forEach

 var dataTable = { "Focus": { "id": 2, "brand": "Ford", "name": "Focus" } } var keys = Object.keys(dataTable); var str = ''; keys.forEach(function(item) { str += dataTable[item].brand + " " + dataTable[item].name; }); alert(str); 

Using for-loop : 使用for-loop

 var dataTable = { "Focus": { "id": 2, "brand": "Ford", "name": "Focus" } } var keys = Object.keys(dataTable); var str = ''; for (var i = 0, len = keys.length; i < len; i++) { str += dataTable[keys[i]].brand + " " + dataTable[keys[i]].name; } alert(str); 

The correct syntax to write this would be: 编写此代码的正确语法为:

When you loop, you'll get the key name in the variable entity and then use that to get the value, also, you need to access the associative array inside the first key ie Focus 循环时,将在变量实体中获取键名,然后使用该键名获取值,此外,还需要访问第一个键(即Focus)内的关联数组

var dataTable = JSON.parse('{"Focus":{"id":2,"brand":"Ford","name":"Focus"}}');
var formattedText = '';
for (var entity in dataTable.Focus) {
    formattedText += dataTable.Focus['brand'] + " " + dataTable.Focus['name'] + "<br>";
}

Sounds like you're using each function in a wrong way. 听起来您在错误地使用每个功能。 in your each function change arguments to key and value like this: 在每个函数中,将参数更改为键和值,如下所示:

$.each(dataTable, function (key, value) {
     //access key and values here
});

In your case u should iterate again over key and values of your key values. 在您的情况下,您应该再次遍历键和键值的值。

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

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