如何创建从另一个对象数组迭代的新对象

时间:2020-07-15 10:11:11

标签: javascript ecmascript-6

const datax = [
        {
          hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
          hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
          hrInTotal: 1106,
          hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
          hrTotal: 10020,
          mo: 5,
          time: "Thu Jun 25 18:30:00 UTC 2020",
        },
        {
          hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
          hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
          hrInTotal: 1106,
          hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
          hrTotal: 10120,
          mo: 5,
          time: "Thu Jun 26 18:30:00 UTC 2020",
        }, {
          hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
          hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
          hrInTotal: 1106,
          hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
          hrTotal: 1020,
          mo: 5,
          time: "Thu Jun 27 18:30:00 UTC 2020",
        },
        {
          hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
          hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
          hrInTotal: 1106,
          hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
          hrTotal: 10110,
          mo: 5,
          time: "Thu Jun 28 18:30:00 UTC 2020",
        }
]
const newArray = datax.foreach((element, index) => {
        const labels = []
        const counts = []
        const idx = index
        labels[idx] = index
        counts[idx] = element.hrTotal
        return {labels, counts}
      });

试图达到以下目的。我想遍历对象数组的上方并获取具有给定结果的新对象数组,以下是我尝试使用foreach的方法,由于forach不是函数,因此出现了错误。

 newArray = [{
               hrTatal:[1020,10110,10120,10020],
               labels:[0,1,2,3]
              }]

2 个答案:

答案 0 :(得分:1)

尝试使用map代替forEach,例如:

const newArray = datax.map((element, index) => {
        const labels = []
        const counts = []
        const idx = index
        labels[idx] = index
        counts[idx] = element.hrTotal
        return {labels, counts}
      });

原因是因为forEach不返回任何内容(undefined)。 map返回修改后的新数组。

答案 1 :(得分:0)

使用reduce,在id内定义输出数组,并附加所需的属性。

const datax = [{
    hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
    hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
    hrInTotal: 1106,
    hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
    hrTotal: 10020,
    mo: 5,
    time: "Thu Jun 25 18:30:00 UTC 2020",
  },
  {
    hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
    hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
    hrInTotal: 1106,
    hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
    hrTotal: 10120,
    mo: 5,
    time: "Thu Jun 26 18:30:00 UTC 2020",
  }, {
    hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
    hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
    hrInTotal: 1106,
    hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
    hrTotal: 1020,
    mo: 5,
    time: "Thu Jun 27 18:30:00 UTC 2020",
  },
  {
    hrCounts: [96, 62, 50, 68, 93, 109, 91, 66, 83, 116, 85, 101],
    hrInCounts: [95, 76, 85, 99, 105, 123, 78, 60, 96, 100, 109, 80],
    hrInTotal: 1106,
    hrLabels: [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26],
    hrTotal: 10110,
    mo: 5,
    time: "Thu Jun 28 18:30:00 UTC 2020",
  }
]


const res = datax.reduce((acc, x, i) => {
  acc[0] = {
    hrTotal: [...acc[0].hrTotal, x.hrTotal],
    labels: [...acc[0].labels, i]
  }
  return acc;
}, [{
  hrTotal: [],
  labels: []
}])

console.log(res)