[英]how to create new object iterate from another array of object
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}
});
Trying to achieve below object.试图在 object 下实现。 I want iterate above array of object and get new array of object with given result below i have tryed using foreach and i am getting error as forach is not a function.我想遍历 object 数组并获得 object 的新数组,下面给出的结果我尝试使用 foreach 并且我收到错误,因为 forach 不是 function。
newArray = [{
hrTatal:[1020,10110,10120,10020],
labels:[0,1,2,3]
}]
Try to use map
instead of forEach
, like:尝试使用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}
});
The reason is because forEach does not return anything ( undefined
).原因是因为forEach不返回任何东西( undefined
)。 map returns a new modified array. map返回一个新的修改数组。
Use reduce, define the output array inside id and append the needed properties.使用 reduce,在 id 内定义 output 数组和 append 所需的属性。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.