I have an Array of Object. Each Object contains another inner Array. I would like to take each inner array as outer object and put the remaining old outer elements as sub properties of newly created outer object.
Input
data = [
{
name: "Sam",
ssn: 123,
age: 25,
hobbies: [{ name: "cricket" }, { name: "football" }]
},
{
name: "John",
ssn: 234,
age: 25,
hobbies: [{ name: "cricket" }, { name: "football" }]
},
{
name: "Mathew",
ssn: 345,
age: 25,
hobbies: [{ name: "cricket" }, { name: "football" }, {name: "carroms"}]
}
];
Expected Output
[
{
name: "cricket",
person_details: [
{ name: "Sam", ssn: 123, age: 25 },
{ name: "John", ssn: 234, age: 25 },
{ name: "Mathew", ssn: 345, age: 25 }
]
},
{
name: "football",
person_details: [
{ name: "Sam", ssn: 123, age: 25 },
{ name: "John", ssn: 234, age: 25 },
{ name: "Mathew", ssn: 345, age: 25 }
]
},
{
name: "carroms",
person_details: [
{ name: "Mathew", ssn: 345, age: 25 }
]
}
]
What i have tried using Reduce as follows
this.data = this.data.reduce(
(a, x) => [...x.hobbies.map(h => ({ ...x, hobbies: [h] }))],
[]
);
You could reduce the array and look for same name items for the result set.
var data = [{ name: "Sam", ssn: 123, age: 25, hobbies: [{ name: "cricket" }, { name: "football" }] }, { name: "John", ssn: 234, age: 25, hobbies: [{ name: "cricket" }, { name: "football" }] }, { name: "Mathew", ssn: 345, age: 25, hobbies: [{ name: "cricket" }, { name: "football" }, { name: "carroms" }] }],
result = data.reduce((r, { hobbies, ...o }) => {
hobbies.forEach(({ name }) => {
var group = r.find(q => q.name === name);
if (!group) r.push(group = { name, person_details: [] });
group.person_details.push(o);
})
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }