gpt4 book ai didi

javascript - 将嵌套数组中的数据合并到一个数组中

转载 作者:行者123 更新时间:2023-12-01 03:54:16 25 4
gpt4 key购买 nike

我有一个如下所示的数据集(第二个代码)。我想将所有数据合并到一个大对象中,请参见下面的示例。我尝试用 forEach 来做到这一点,但我没有得到正确的结果。

我想要实现的目标:

 var arr2 = [{
"date": "20170314",
"steps": 620,
"nutrition.calories": 1634,
"nutrition.fat.total": 57.22602462768555,
"nutrition.protein": 188.070068359375,
"nutrition.carbs.total": 83.85400390625
}, {
"date": "20170314",
"steps": 620,
"nutrition.calories": 1634,
"nutrition.fat.total": 57.22602462768555,
"nutrition.protein": 188.070068359375,
"nutrition.carbs.total": 83.85400390625
}]

这就是我当前数组的样子:

var array = [
{
"type": "steps",
"data": [
{
"startDate": "2017-03-12T23:00:00.000Z",
"endDate": "2017-03-13T23:00:00.000Z",
"value": 1031,
"unit": "count"
}, {
"startDate": "2017-03-13T23:00:00.000Z",
"endDate": "2017-03-14T23:00:00.000Z",
"value": 620,
"unit": "count"
}
]
}, {
"type": "nutrition.calories",
"data": [
{
"startDate": "2017-03-12T23:00:00.000Z",
"endDate": "2017-03-13T23:00:00.000Z",
"value": 0,
"unit": "kcal"
}, {
"startDate": "2017-03-13T23:00:00.000Z",
"endDate": "2017-03-14T23:00:00.000Z",
"value": 1634.100463867188,
"unit": "kcal"
}
]
}, {
"type": "nutrition.fat.total",
"data": [
{
"startDate": "2017-03-12T23:00:00.000Z",
"endDate": "2017-03-13T23:00:00.000Z",
"value": 0,
"unit": "g"
}, {
"startDate": "2017-03-13T23:00:00.000Z",
"endDate": "2017-03-14T23:00:00.000Z",
"value": 57.22602462768555,
"unit": "g"
}
]
}, {
"type": "nutrition.protein",
"data": [
{
"startDate": "2017-03-12T23:00:00.000Z",
"endDate": "2017-03-13T23:00:00.000Z",
"value": 0,
"unit": "g"
}, {
"startDate": "2017-03-13T23:00:00.000Z",
"endDate": "2017-03-14T23:00:00.000Z",
"value": 188.070068359375,
"unit": "g"
}
]
}, {
"type": "nutrition.carbs.total",
"data": [
{
"startDate": "2017-03-12T23:00:00.000Z",
"endDate": "2017-03-13T23:00:00.000Z",
"value": 0,
"unit": "g"
}, {
"startDate": "2017-03-13T23:00:00.000Z",
"endDate": "2017-03-14T23:00:00.000Z",
"value": 83.85400390625,
"unit": "g"
}
]
}
]

有人可以帮我吗? Foreach 循环不工作。

卡布

最佳答案

我注意到原始数组中的第一个 Data 对象除了步骤之外的每个属性都有 0 值,如果这对于输出数组来说是正确的而不是您最初提供的答案,那么这可能是解决方案:

var originalArray = [
{
"type": "steps",
"data": [
{
"startDate": "2017-03-12T23:00:00.000Z",
"endDate": "2017-03-13T23:00:00.000Z",
"value": 1031,
"unit": "count"
}, {
"startDate": "2017-03-13T23:00:00.000Z",
"endDate": "2017-03-14T23:00:00.000Z",
"value": 620,
"unit": "count"
}
]
}, {
"type": "nutrition.calories",
"data": [
{
"startDate": "2017-03-12T23:00:00.000Z",
"endDate": "2017-03-13T23:00:00.000Z",
"value": 0,
"unit": "kcal"
}, {
"startDate": "2017-03-13T23:00:00.000Z",
"endDate": "2017-03-14T23:00:00.000Z",
"value": 1634.100463867188,
"unit": "kcal"
}
]
}, {
"type": "nutrition.fat.total",
"data": [
{
"startDate": "2017-03-12T23:00:00.000Z",
"endDate": "2017-03-13T23:00:00.000Z",
"value": 0,
"unit": "g"
}, {
"startDate": "2017-03-13T23:00:00.000Z",
"endDate": "2017-03-14T23:00:00.000Z",
"value": 57.22602462768555,
"unit": "g"
}
]
}, {
"type": "nutrition.protein",
"data": [
{
"startDate": "2017-03-12T23:00:00.000Z",
"endDate": "2017-03-13T23:00:00.000Z",
"value": 0,
"unit": "g"
}, {
"startDate": "2017-03-13T23:00:00.000Z",
"endDate": "2017-03-14T23:00:00.000Z",
"value": 188.070068359375,
"unit": "g"
}
]
}, {
"type": "nutrition.carbs.total",
"data": [
{
"startDate": "2017-03-12T23:00:00.000Z",
"endDate": "2017-03-13T23:00:00.000Z",
"value": 0,
"unit": "g"
}, {
"startDate": "2017-03-13T23:00:00.000Z",
"endDate": "2017-03-14T23:00:00.000Z",
"value": 83.85400390625,
"unit": "g"
}
]
}
]

var res = originalArray.reduce(function(result, obj){
obj.data.forEach(function(dataObj, index){
if(!result[index]){
result[index] = {
date: dataObj.startDate.split('T')[0].split('-').join('')
}
}
result[index][obj.type] = dataObj.value;
})

return result;
},[])

console.log(res)

关于javascript - 将嵌套数组中的数据合并到一个数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42902538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com