0

I have data like this:

[{ team: 'Team1',
     wins: 1,
     group: GroupA
},{ team: 'Team2',
     wins: 1,
     group: GroupA
},{ team: 'Team3',
     wins: 1,
     group: GroupB
},{ team: 'Team4',
     wins: 1,
     group: GroupB
}]

I want it in this form (I basically want to group the data by some value, in this case "group" and use that value as a key in an object of arrays):

{ GroupA: [{'Team1', wins: 1, group: GroupA},{'Team2', wins: 1, group: GroupA}],
GroupB: [{'Team3', wins: 1, group: GroupB},{'Team4', wins: 1, group: GroupB}]
}

How can I accomplish this?

Here is what I tried and which almost worked, but returns objects:

var newStats = {}
     arrTeamStats.map(function(key,idx){

                var arr = [];

                if(newStats[key.group] == undefined) {
                    arr = key;
                    newStats[key.group] = arr;
                } else {
                else {
                    group = key.group;
                    newStats[group].push(key);
                }
           }

3 Answers 3

4

You need to reduce your array to return an Object.

arrTeamStats.reduce(function(h, o) {
  h[o.group] = h[o.group] || []
  h[o.group].push(o)
  return h
}, {})

HTH

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I forgot to mention I want a vanilla javascript solution. I don't mind some of the newer syntax but I want to the plain one too.
Changed. Force of habit.
1

First, you're misunderstanding .map(). It's not a general purpose iterator, but rather is used to create a new array from the content of an existing one.

What you can do is simply iterate and add new properties to an object as they're located, and consolidate the content.

var data = [{ team: 'Team1',wins: 1,group: "GroupA"},{ team: 'Team2',wins: 1,group: "GroupA"},{ team: 'Team3',wins: 1,group: "GroupB"},{ team: 'Team4',wins: 1,group: "GroupB"}];

var result = {};
for (const o of data) {
  if (result.hasOwnProperty(o.group)) result[o.group].push([o]);
  else result[o.group] = [[o]];
}

console.log(result);


Or a little more concise like this:

var data = [{ team: 'Team1',wins: 1,group: "GroupA"},{ team: 'Team2',wins: 1,group: "GroupA"},{ team: 'Team3',wins: 1,group: "GroupB"},{ team: 'Team4',wins: 1,group: "GroupB"}];

var result = {};
for (const o of data) result[o.group] = (result[o.group] || []).concat([[o]]);

console.log(result);

7 Comments

How can I tweak this to get { "GroupA": [{ "team": "Team1", "wins": 1, "group": "GroupA" }], [{ "team": "Team2", "wins": 1, "group": "GroupA" }], "GroupB": [{ "team": "Team3", "wins": 1, "group": "GroupB" }], [{ "team": "Team4", "wins": 1, "group": "GroupB" }] }
@mo_maat: That's the result it currently gives. I'm not seeing any difference. Can you explain what you mean?
It's maybe hard to notice but it is different. I need separate arrays for the elements within the group: So GroupA: [{element1}], [{element2}] instead of GroupA: [{element1}, {element2}]
Oh, I see. That won't work because it's not a valid structure. You'd need to wrap each individual array in an array that holds them all. { "GroupA": [[{ "team": "Team1", "wins": 1, "group": "GroupA" }], [{ "team": "Team2", "wins": 1, "group": "GroupA" }]], "GroupB": ...
... GroupA: [[{element1}], [{element2}]], GroupB: ...
|
0

If you're inclined to use something like Lodash, you could use _.groupBy as follows:

var input = [{ team: 'Team1',
     wins: 1,
     group: 'GroupA'
},{ team: 'Team2',
     wins: 1,
     group: 'GroupA'
},{ team: 'Team3',
     wins: 1,
     group: 'GroupB'
},{ team: 'Team4',
     wins: 1,
     group: 'GroupB'
}];

var output = _.groupBy(input, 'group');
console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

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.