my array that i download from the server looks like this,
array=[ { icon: 'm-warning',
color: '#FDC204',
name: 'exp1',
team: 'c',
user: 'alpha' },
{ icon: 'm-puzzle',
color: '#86AF45',
name: 'exp2',
team: 'c',
user: 'alpha' },
{ icon: 'm-refresh',
color: '#77A746',
name: 'exp3',
team: 'c',
user: 'alpha' },
{ icon: 'm-puzzle',
color: '#86AF45',
name: 'exp3',
team: 'a',
user: 'beta' },
{ icon: 'm-clock',
color: '#4677A7',
name: 'exp4',
team: 'b',
user: 'beta' },
{ icon: 'm-warning',
color: '#FDC204',
name: 'exp5',
team: 'a',
user: 'gamma' } ]
If i need to create a nested object( # of exps, belonging to respective teams, organized by user) from the above array how would i go about it. my resulting nested object shud look like this, (notice alpha has 3 exps belonging to team c) and so on and so forth
[{
id: 'gamma',
data: [
['a', 1]
]
},
{
id: 'beta',
data: [
['a', 1],
['b', 1]
]
},
{
id: 'alpha',
data: [
['c', 3]
]
}]
you can do this traditionally with couple of for loops. but is there a easier way to reduce the for loop cycles and get this created. the caveat is, this must be strict JS I do not know how many users/teams/exps there might be. there may be 50 different users/1000 different experiments . So i can't hardcode any exp names/users/ etc.
data['alpha']is far easier to deal with than having to scan your parent array every time to figure out which indexid:alphais in.