javascript - 如何从另一个对象数组迭代创建新对象
问题描述
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]
}]
解决方案
推荐阅读
- javascript - 对于 post 语句中的变量的循环添加赋值
- python - 创建一个折线图,x 轴为日期时间,y 轴为记录数
- html - 在 Angular 中使用在线 API
- php - 手动创建 UploadedFile
- javascript - 动画 D3.js 条形图。从底部到顶部。请检查链接以获取参考图片
- json - REST-Api 中传递结构化数据或键值对的最佳实践是什么?
- extjs - ExtJSv7:实现 Ext.mixin.Observable 时出错
- reactjs - 无效的 latlng 对象 - React Leaflet
- python - 如何为 python3 安装 netfilterqueue?
- r - data.frame 尝试将变量名称分配为标签而不是变量内容?