简体   繁体   English

访问JSON嵌套数据

[英]Accessing JSON nested data

I'm trying to access the values node in the following JSON document using something like this (assume data is the JSON document): 我正在尝试使用以下方式访问以下JSON文档中的values节点(假设数据是JSON文档):

console.log(data.values);

Is this because the document is one big array? 这是因为文档是一个大数组吗?

[
  {
     "key": "Failed",
     "vis": "1",
     "values": [
        {
           "date": "2014-02-26T05:00:00.000Z",
           "total": 0.07142857142857142
        }
     ]
  },
  {
     "key": "Pass",
     "vis": "1",
     "values": [
        {
           "date": "2014-02-26T05:00:00.000Z",
           "total": 1.325781
        }
     ]
  },
]

Because you're dealing with a nested array structure, you'd have to do something like below to extract and aggregate the values into a more accessible collection 因为您要处理的是嵌套数组结构,所以您必须执行以下类似操作以将值提取并聚合到更易于访问的集合中

var objects = [
  {
     "key": "Failed",
     "vis": "1",
     "values": [
        {
           "date": "2014-02-26T05:00:00.000Z",
           "total": 0.07142857142857142
        }
     ]
  },
  {
     "key": "Pass",
     "vis": "1",
     "values": [
        {
           "date": "2014-02-26T05:00:00.000Z",
           "total": 1.325781
        }
     ]
  },
];

var aggregates = [];

objects.forEach(function(o, i){
   o.values.forEach(function(v, i){
      aggregates.push(v);
   });
});

console.log(aggregates);

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

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