我有一个如下所示的数据集(第二个代码)。我想将所有数据合并到一个大对象中,请参见下面的示例。我尝试使用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)

07-24 09:47
查看更多