0

I have a dataset that looks like below, (second code). I want to merge all my data to one big object, see example below. I tried to do it with a forEach, but I'm not getting the right results back.

What I want to achieve:

 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
      }]

This is how my current array looks like:

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"
      }
    ]
  }
]

Can someone please help me with it. Foreach loop is not working.

Kab

0

2 Answers 2

2

I noticed that the first Data object in the original array has 0 values for every property except for the step, if that is correct for the output array not the answer that you originally provided than this could be the solution:

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Ilija, I couldn't apply the correct formatting from the mobile website.
@kabus: Accept and Upvote if the solution works for you. No need for thank you comments.
0

I think you are missing some constrains.

For instance, should the data be attributed to the startDate or endDate?

Following an assumption of attributing to the endDate, I think this might help you:

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);

which results in:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.