如何将两个json数组合并为这种格式之一

时间:2018-06-01 15:35:40

标签: javascript arrays json node.js merge

First Array

{categories":[{"label":"2018-05-24T12:04:01.000Z"},{"label":"2018-05-24T12:05:03.000Z"},{"label":"2018-05-24T12:08:08.000Z"},{"label":"2018-05-24T12:09:10.000Z"},{"label":"2018-05-24T12:11:14.000Z"},{"label":"2018-05-24T12:12:15.000Z"},{"label":"2018-05-24T12:13:17.000Z"},{"label":"2018-05-24T12:15:21.000Z"},{"label":"2018-05-24T12:16:22.000Z"},{"label":"2018-05-24T12:17:24.000Z"},{"label":"2018-05-24T12:17:26.000Z"},{"label":"2018-05-24T12:18:28.000Z"},{"label":"2018-05-24T12:19:30.000Z"},{"label":"2018-05-24T12:20:31.000Z"},{"label":"2018-05-24T12:21:33.000Z"}]}

第二个数组

{data":[{"value":23.44},{"value":23.43},{"value":23.42},{"value":23.4},{"value":23.4},{"value":23.38},{"value":23.37},{"value":23.36},{"value":23.35},{"value":23.38},{"value":23.4},{"value":23.4},{"value":23.38},{"value":23.39},{"value":23.37},{"value":23.35},{"value":23.34},{"value":23.35},{"value":23.34},{"value":23.32},{"value":23.35},{"value":23.35},{"value":23.35},{"value":23.35},{"value":23.35},{"value":23.37},{"value":23.3},{"value":23.35},{"value":23.35},{"value":23.32},{"value":23.34},{"value":23.33},{"value":23.35},{"value":23.32},{"value":23.34},{"value":23.35},{"value":23.32},{"value":23.34},{"value":23.35}]}

结果数组:

[[2018-05-24T12:04:01.000Z , 23.44 ] , [2018-05-24T12:05:03.000Z , 23.43],.....]

1 个答案:

答案 0 :(得分:0)

const result = categories.map(({label}, index) => ([label, data[index].value]));

循环浏览类别并在同一位置添加数据。