使用 forloop 获取值

拉克什K

我写了一个代码,它返回一个json,如下所示。

"aggrRes": {
"totalSize": 3,
"done": true,
"records": [
  {
    "attributes": {
      "type": "AggregateResult"
    },
    "expr0": "2019-02-25",
    "expr1": 40,
    "expr2": 27
  },
  {
    "attributes": {
      "type": "AggregateResult"
    },
    "expr0": "2019-02-22",
    "expr1": 36,
    "expr2": 9
  },
  {
    "attributes": {
      "type": "AggregateResult"
    },
    "expr0": "2019-02-21",
    "expr1": 38,
    "expr2": 12.5
  }
 ]
}

使用这个 json 我想创建另一个 json。这里的格式应该如下所示。

in forloop:
   dataset:
      dates:[get all expr0 for all 3 records],
      expr1:[get all expr1 for all 3 records],
      expr2:[get all expr2 for all 3 records]

在这里,我感到困惑的主要部分是如何执行以下操作。

var agResp = this.total_info.aggrRes;
for(int i=0; i<records.size; i++){
console.log(agResp.records[0].expr**i**);
}

在这里,我想通过循环值来获取expri,这将生成expr0expr1expr2值。

谢谢

赞恩·扎法尔
var records = aggrRes.records; // Store Records in a variable
var values = [];

records.forEach(record => {
  Object.keys(record).forEach(key => {
    if (values[key]) {
      values[key].push(record[key]);
    } else {
      values[key] = [record[key]];
    }
  });
});
console.log(values);

会返回这样的东西

attributes: (3) [{…}, {…}, {…}]
expr0: (3) ["2019-02-25", "2019-02-22", "2019-02-21"]
expr1: (3) [40, 36, 38]
expr2: (3) [27, 9, 12.5]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章