How can JavaScript iterate through an Array of Objects and return a new Object with merged arrays based on Object keys?
The original Array of Objects is as follows:
this.obj = [
{
"name": "test name 1",
"teams": [{
"manage": false,
"name": "my test team",
}]
},
{
"name": "test name 2",
"teams": [{
"manage": false,
"name": "TEAM2",
}]
}
];
The expected result should be:
{
"teams": [{
"manage": true,
"name": "TEAM2",
}, {
"manage": false,
"name": "my test team",
}]
}
I was able to achieve this using two nested loops and a variable. How can this scenario be approached in JavaScript in a more efficient way?
let data = {'teams': []};
for (var i = this.obj.length - 1; i >= 0; i--) {
for (var p = this.obj[i].teams.length - 1; p >= 0; p--) {
data.teams.push(this.groups[i].teams[p]);
}
}