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

时间:2017-03-20 11:34:53

标签: javascript arrays typescript

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

KAB

2 个答案:

答案 0 :(得分:2)

我注意到原始数组中的第一个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)

答案 1 :(得分:0)

我认为你缺少一些限制。

例如,数据应该归因于startDate还是endDate

假设归因于endDate,我认为这可能会对您有所帮助:

var models = {};
array.forEach(function(a) {
    a.data.forEach(function(e, idx) {
        if (!(e.endDate in models)) {
            models[e.endDate] = {};
        }
        if (!(a.type in models[e.endDate])) {
            models[e.endDate][a.type] = 0;
        }
        models[e.endDate][a.type] = e.value;
    });
});
models = Object.keys(models).map(function(k){ models[k].date = k.slice(0,10).replace(/-/g, ''); return models[k]; });
console.log(models);

导致:

enter image description here