映射此JSON数组的Javascript语法是什么?

时间:2018-12-12 17:38:51

标签: javascript arrays

我有一个React组件,通过“ get”调用提取JSON数据。这是检索到的数据的格式:

Validate

我已经使用Object(keys)函数成功创建了devices_by_type名称的新数组。现在,我需要做的是建立另外两个数组,分别是失败的和总数为NUMBERS的数组,我不确定该怎么做。

例如,我的“失败”阵列为:

const failedNumbers = [10、3、123等],以及总数。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

在修复丢失的端支架后使用reduce

const obj = {
  "id": "5c114382-f9da-4dc2-8371-339659a1c8ec",
  "resourceId": "5c0a7671-f821-4131-9473-3fd1e80dbac1",
  "interface": "get_metrics",
  "inputs": {
    "stage": "TEST"
  },
  "outputs": {
    "metrics": {
      "devices_by_type": {
        "MX480": {
          "failed": 10,
          "total": 1235
        },
        "EX4200": {
          "failed": 3,
          "total": 1490
        },
        "ETX-203AX": {
          "failed": 123,
          "total": 9643
        },
        "MX960": {
          "failed": 52,
          "total": 1211
        },
        "GE114": {
          "failed": 1200,
          "total": 12530
        },
        "QFX5100": {
          "failed": 100,
          "total": 950
        },
        "MX240": {
          "failed": 10,
          "total": 245
        },
        "GE114Pro": {
          "failed": 0,
          "total": 125
        }
      },
      "state": "successful",
      "reason": "",
      "progress": [],
      "providerData": {},
      "createdAt": "2018-12-12T17:21:06.174Z",
      "updatedAt": "2018-12-12T17:21:06.918Z",
      "resourceStateConstraints": {},
      "executionGroup": "lifecycle"
    }
  }
}

const devices = obj.outputs.metrics.devices_by_type;
const fails = Object.keys(devices).reduce(function(result, key) {
  result.push(devices[key].failed);
  return result;
}, []);
console.log(fails);