-2

I have structured JSON data as below:

[
    {
        "Month": "Jan"
        "Year": 2015,
        "Count": 15
    },
    {
        "Month": "Feb"
        "Year": 2015,
        "Count": 5
    },
    {
        "Month": "Jan"
        "Year": 2016,
        "Count": 25
    },
        {
        "Month": "Feb"
        "Year": 2016,
        "Count": 50
    }
]

I want to group the data using JavaScript to get the below output. Can someone help me with this?

[
    {
        "Month": "Jan"
        "Count_2015": 15,
        "Count_2016": 25
    },
    {
        "Month": "Feb"
        "Count_2015": 5,
        "Count_2016": 50
    }
]
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
user7530819
  • 25
  • 1
  • 3
  • Check out this question. it might be helpful https://stackoverflow.com/questions/38575721/grouping-json-by-values – Codecookie1490 Jul 06 '17 at 19:39
  • 2
    Please see [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236). – Sebastian Simon Jul 06 '17 at 19:39
  • Iterate over the JSON and as you do test for your conditions and add it all to a new JSON object. Obviously you'll have to create the keys from the value and it will require a bit of recursion. – Difster Jul 06 '17 at 19:41
  • Please post what you've tried and then ask why it doesn't work – AlexM Jul 06 '17 at 19:42
  • Possible duplicate of [Grouping JSON by values](https://stackoverflow.com/questions/38575721/grouping-json-by-values) – Kukic Vladimir Jul 06 '17 at 19:42
  • 1
    Stackoverflow is not a free code writing service. There are lots and lots of other similar questions around you could research to at least get a starting point. Then when you have real code problems ask questions then – charlietfl Jul 06 '17 at 19:44

2 Answers2

1

You can use forEach() loop and use one object as thisArg param.

var data = [{"Month":"Jan","Year":2015,"Count":15},{"Month":"Feb","Year":2015,"Count":5},{"Month":"Jan","Year":2016,"Count":25},{"Month":"Feb","Year":2016,"Count":50}]  

var result = []
data.forEach(function(e, i) {
  if (!this[e.Month]) {
    this[e.Month] = {
      Month: e.Month,
      ['Count_' + e.Year]: e.Count
    }
    result.push(this[e.Month])
  } else {
    this[e.Month]['Count_' + e.Year] = e.Count
  }
}, {})

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
  • Thanks Nenad. I will try this out. – user7530819 Jul 06 '17 at 20:12
  • Sorry everyone. I am relatively new to this and will make sure the next time I face an issue, i will word the question properly and given more clarity with what I have tried out and what issue I faced. – user7530819 Jul 06 '17 at 20:14
  • Nice solution - I prefer your answer. But you don't need the i arg, it's superfluous. – JGFMK Jul 06 '17 at 20:41
0
var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var result = [];
[].forEach.call(months, function(month) {
  var monthObj = {"Month": month};
  result.push(monthObj);
});
let mi, year, keystr
for (var i=0; i< data.length; i++) {
  var obj = data[i];
  for (var key in obj) {
    if (!obj.hasOwnProperty(key)) continue;
    var value = obj[key];
    switch (key) {
      case "Month":
        mi = months.indexOf(value);
        break;
      case "Year":
        year = value;
        break;
      case "Count":
        keystr = "Count_" + year.toString();
        resObj = result[mi];
        resObj[keystr] = value;
        break;
    }
  }
}
console.log(result);

Try it here

JGFMK
  • 8,425
  • 4
  • 58
  • 92
  • In my version I tried to make sure the months displayed in chronological order in the result set, rather than presume on complete set for all months for each year, so if you processed "Sep" first, it would be first result... – JGFMK Jul 06 '17 at 21:19