Reduce | const data = [
{
"adult": false,
"gender": 2,
"id": 137427,
"known_for_department": "Directing",
"name": "Denis Villeneuve",
"original_name": "Denis Villeneuve",
"popularity": 38.749,
"profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
"credit_id": "5e959beedb72c00012ad7dcf",
"department": "Directing",
"job": "Director"
},
{
"adult": false,
"gender": 2,
"id": 564940,
"known_for_department": "Writing",
"name": "Jon Spaihts",
"original_name": "Jon Spaihts",
"popularity": 10.146,
"profile_path": "/jzIwgRpEG2VXYHmIyk0vpb6S3TA.jpg",
"credit_id": "5e9ca1bfe22d28001b61990e",
"department": "Writing",
"job": "Screenplay"
},
{
"adult": false,
"gender": 2,
"id": 12506,
"known_for_department": "Writing",
"name": "Frank Herbert",
"original_name": "Frank Herbert",
"popularity": 10.333,
"profile_path": "/1uE09gzcwXMjCfXZfXXwNDIHaXG.jpg",
"credit_id": "5e9ca1974a4bfc001cd6cbdc",
"department": "Writing",
"job": "Novel"
},
{
"adult": false,
"gender": 2,
"id": 137427,
"known_for_department": "Directing",
"name": "Denis Villeneuve",
"original_name": "Denis Villeneuve",
"popularity": 38.749,
"profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
"credit_id": "6021f06b458199003da81ad8",
"department": "Production",
"job": "Producer"
},
{
"adult": false,
"gender": 1,
"id": 114408,
"known_for_department": "Production",
"name": "Mary Parent",
"original_name": "Mary Parent",
"popularity": 5.667,
"profile_path": "/4SiCFwZhHwai9zL0GlcjAbMeBnL.jpg",
"credit_id": "614bd5002b531d002943b865",
"department": "Production",
"job": "Producer"
},
{
"adult": false,
"gender": 2,
"id": 17315,
"known_for_department": "Production",
"name": "Cale Boyter",
"original_name": "Cale Boyter",
"popularity": 4.018,
"profile_path": "/efBT8dyE60r4MtEMhoWjjrnepBu.jpg",
"credit_id": "614bd50ec7176d002da094d6",
"department": "Production",
"job": "Producer"
},
{
"adult": false,
"gender": 0,
"id": 999565,
"known_for_department": "Editing",
"name": "Joe Walker",
"original_name": "Joe Walker",
"popularity": 5.457,
"profile_path": "/2pfPFxb2I0ulvMZx5B7juwvuU3I.jpg",
"credit_id": "614bd5bda6ddcb00621c6ded",
"department": "Editing",
"job": "Editor"
},
{
"adult": false,
"gender": 2,
"id": 16177,
"known_for_department": "Sound",
"name": "Ron Bartlett",
"original_name": "Ron Bartlett",
"popularity": 6.699,
"profile_path": "/AmFG81joHLkvnh4Io20TdCCBk8P.jpg",
"credit_id": "614c05f0d2c0c100453e42d3",
"department": "Sound",
"job": "Sound Re-Recording Mixer"
},
{
"adult": false,
"gender": 2,
"id": 947,
"known_for_department": "Sound",
"name": "Hans Zimmer",
"original_name": "Hans Zimmer",
"popularity": 21.646,
"profile_path": "/tpQnDeHY15szIXvpnhlprufz4d.jpg",
"credit_id": "614c05fc4ccc50008695fe6d",
"department": "Sound",
"job": "Original Music Composer"
},
{
"adult": false,
"gender": 2,
"id": 102873,
"known_for_department": "Art",
"name": "Patrice Vermette",
"original_name": "Patrice Vermette",
"popularity": 6.082,
"profile_path": "/8PSZsickII5OpDEzcBqhx5aGy46.jpg",
"credit_id": "614c0716c7176d002da123f2",
"department": "Art",
"job": "Production Design"
},
{
"adult": false,
"gender": 2,
"id": 1982162,
"known_for_department": "Visual Effects",
"name": "Paul Lambert",
"original_name": "Paul Lambert",
"popularity": 2.327,
"profile_path": "/w3J8GNKqhgYCtdfwCEBZnEwwJu0.jpg",
"credit_id": "614c083e594c940088b28931",
"department": "Visual Effects",
"job": "Visual Effects Supervisor"
},
{
"adult": false,
"gender": 2,
"id": 67113,
"known_for_department": "Camera",
"name": "Greig Fraser",
"original_name": "Greig Fraser",
"popularity": 11.461,
"profile_path": "/b9q2uhoS8M4LBuD0ymNoGI5lMs4.jpg",
"credit_id": "614c09f1609750005f91736f",
"department": "Camera",
"job": "Director of Photography"
},
{
"adult": false,
"gender": 0,
"id": 1790475,
"known_for_department": "Production",
"name": "Byron Merritt",
"original_name": "Byron Merritt",
"popularity": 1.815,
"profile_path": null,
"credit_id": "614c0a94326ec10064595937",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 2,
"id": 564940,
"known_for_department": "Writing",
"name": "Jon Spaihts",
"original_name": "Jon Spaihts",
"popularity": 10.146,
"profile_path": "/jzIwgRpEG2VXYHmIyk0vpb6S3TA.jpg",
"credit_id": "614c0aa42dffd800668b0ce4",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 0,
"id": 1790474,
"known_for_department": "Production",
"name": "Brian Herbert",
"original_name": "Brian Herbert",
"popularity": 1.78,
"profile_path": "/aUkuwDrFeOZU5BAXk1JeMGC6iL3.jpg",
"credit_id": "614c0ab51689350064f7823f",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 0,
"id": 1790476,
"known_for_department": "Production",
"name": "Kim Herbert",
"original_name": "Kim Herbert",
"popularity": 4.562,
"profile_path": null,
"credit_id": "614c0ae24ccc500029915987",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 2,
"id": 62739,
"known_for_department": "Production",
"name": "Herb Gains",
"original_name": "Herb Gains",
"popularity": 5.403,
"profile_path": null,
"credit_id": "614c0b53326ec1008fcc8223",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 0,
"id": 2364439,
"known_for_department": "Production",
"name": "Joshua Grode",
"original_name": "Joshua Grode",
"popularity": 2.234,
"profile_path": null,
"credit_id": "614c0b66b04228002a111394",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 2,
"id": 54211,
"known_for_department": "Production",
"name": "Thomas Tull",
"original_name": "Thomas Tull",
"popularity": 3.825,
"profile_path": "/5UG4FK7rsmhzJDYxpU28acqfxtu.jpg",
"credit_id": "614c0b86cf4b8b004592546c",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 2,
"id": 137427,
"known_for_department": "Directing",
"name": "Denis Villeneuve",
"original_name": "Denis Villeneuve",
"popularity": 38.749,
"profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
"credit_id": "6178ed77c2b9df0062239903",
"department": "Writing",
"job": "Screenplay"
},
{
"adult": false,
"gender": 2,
"id": 1322142,
"known_for_department": "Art",
"name": "Tom Brown",
"original_name": "Tom Brown",
"popularity": 3.99,
"profile_path": null,
"credit_id": "624075dbc740d9005d719d27",
"department": "Art",
"job": "Supervising Art Director"
},
{
"adult": false,
"gender": 0,
"id": 3482372,
"known_for_department": "Art",
"name": "Miklós Hatvani-Deàk",
"original_name": "Miklós Hatvani-Deàk",
"popularity": 2.157,
"profile_path": null,
"credit_id": "624075e545765d0047d4e7be",
"department": "Art",
"job": "Art Direction"
},
{
"adult": false,
"gender": 0,
"id": 3345289,
"known_for_department": "Art",
"name": "Adorjan Portik",
"original_name": "Adorjan Portik",
"popularity": 2.56,
"profile_path": null,
"credit_id": "624075f0e329430047e3aef4",
"department": "Art",
"job": "Art Direction"
}
]
const mergedData = data.reduce((acc, obj, index) => {
const existingIndex = acc.findIndex(item => item.name === obj.name);
if (existingIndex !== -1) {
acc[existingIndex].job.push(obj.job);
} else {
const newObj = {...obj};
newObj.job = [newObj.job];
acc.push(newObj);
}
return acc;
}, []);
| ready |
Reduce | const data = [
{
"adult": false,
"gender": 2,
"id": 137427,
"known_for_department": "Directing",
"name": "Denis Villeneuve",
"original_name": "Denis Villeneuve",
"popularity": 38.749,
"profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
"credit_id": "5e959beedb72c00012ad7dcf",
"department": "Directing",
"job": "Director"
},
{
"adult": false,
"gender": 2,
"id": 564940,
"known_for_department": "Writing",
"name": "Jon Spaihts",
"original_name": "Jon Spaihts",
"popularity": 10.146,
"profile_path": "/jzIwgRpEG2VXYHmIyk0vpb6S3TA.jpg",
"credit_id": "5e9ca1bfe22d28001b61990e",
"department": "Writing",
"job": "Screenplay"
},
{
"adult": false,
"gender": 2,
"id": 12506,
"known_for_department": "Writing",
"name": "Frank Herbert",
"original_name": "Frank Herbert",
"popularity": 10.333,
"profile_path": "/1uE09gzcwXMjCfXZfXXwNDIHaXG.jpg",
"credit_id": "5e9ca1974a4bfc001cd6cbdc",
"department": "Writing",
"job": "Novel"
},
{
"adult": false,
"gender": 2,
"id": 137427,
"known_for_department": "Directing",
"name": "Denis Villeneuve",
"original_name": "Denis Villeneuve",
"popularity": 38.749,
"profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
"credit_id": "6021f06b458199003da81ad8",
"department": "Production",
"job": "Producer"
},
{
"adult": false,
"gender": 1,
"id": 114408,
"known_for_department": "Production",
"name": "Mary Parent",
"original_name": "Mary Parent",
"popularity": 5.667,
"profile_path": "/4SiCFwZhHwai9zL0GlcjAbMeBnL.jpg",
"credit_id": "614bd5002b531d002943b865",
"department": "Production",
"job": "Producer"
},
{
"adult": false,
"gender": 2,
"id": 17315,
"known_for_department": "Production",
"name": "Cale Boyter",
"original_name": "Cale Boyter",
"popularity": 4.018,
"profile_path": "/efBT8dyE60r4MtEMhoWjjrnepBu.jpg",
"credit_id": "614bd50ec7176d002da094d6",
"department": "Production",
"job": "Producer"
},
{
"adult": false,
"gender": 0,
"id": 999565,
"known_for_department": "Editing",
"name": "Joe Walker",
"original_name": "Joe Walker",
"popularity": 5.457,
"profile_path": "/2pfPFxb2I0ulvMZx5B7juwvuU3I.jpg",
"credit_id": "614bd5bda6ddcb00621c6ded",
"department": "Editing",
"job": "Editor"
},
{
"adult": false,
"gender": 2,
"id": 16177,
"known_for_department": "Sound",
"name": "Ron Bartlett",
"original_name": "Ron Bartlett",
"popularity": 6.699,
"profile_path": "/AmFG81joHLkvnh4Io20TdCCBk8P.jpg",
"credit_id": "614c05f0d2c0c100453e42d3",
"department": "Sound",
"job": "Sound Re-Recording Mixer"
},
{
"adult": false,
"gender": 2,
"id": 947,
"known_for_department": "Sound",
"name": "Hans Zimmer",
"original_name": "Hans Zimmer",
"popularity": 21.646,
"profile_path": "/tpQnDeHY15szIXvpnhlprufz4d.jpg",
"credit_id": "614c05fc4ccc50008695fe6d",
"department": "Sound",
"job": "Original Music Composer"
},
{
"adult": false,
"gender": 2,
"id": 102873,
"known_for_department": "Art",
"name": "Patrice Vermette",
"original_name": "Patrice Vermette",
"popularity": 6.082,
"profile_path": "/8PSZsickII5OpDEzcBqhx5aGy46.jpg",
"credit_id": "614c0716c7176d002da123f2",
"department": "Art",
"job": "Production Design"
},
{
"adult": false,
"gender": 2,
"id": 1982162,
"known_for_department": "Visual Effects",
"name": "Paul Lambert",
"original_name": "Paul Lambert",
"popularity": 2.327,
"profile_path": "/w3J8GNKqhgYCtdfwCEBZnEwwJu0.jpg",
"credit_id": "614c083e594c940088b28931",
"department": "Visual Effects",
"job": "Visual Effects Supervisor"
},
{
"adult": false,
"gender": 2,
"id": 67113,
"known_for_department": "Camera",
"name": "Greig Fraser",
"original_name": "Greig Fraser",
"popularity": 11.461,
"profile_path": "/b9q2uhoS8M4LBuD0ymNoGI5lMs4.jpg",
"credit_id": "614c09f1609750005f91736f",
"department": "Camera",
"job": "Director of Photography"
},
{
"adult": false,
"gender": 0,
"id": 1790475,
"known_for_department": "Production",
"name": "Byron Merritt",
"original_name": "Byron Merritt",
"popularity": 1.815,
"profile_path": null,
"credit_id": "614c0a94326ec10064595937",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 2,
"id": 564940,
"known_for_department": "Writing",
"name": "Jon Spaihts",
"original_name": "Jon Spaihts",
"popularity": 10.146,
"profile_path": "/jzIwgRpEG2VXYHmIyk0vpb6S3TA.jpg",
"credit_id": "614c0aa42dffd800668b0ce4",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 0,
"id": 1790474,
"known_for_department": "Production",
"name": "Brian Herbert",
"original_name": "Brian Herbert",
"popularity": 1.78,
"profile_path": "/aUkuwDrFeOZU5BAXk1JeMGC6iL3.jpg",
"credit_id": "614c0ab51689350064f7823f",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 0,
"id": 1790476,
"known_for_department": "Production",
"name": "Kim Herbert",
"original_name": "Kim Herbert",
"popularity": 4.562,
"profile_path": null,
"credit_id": "614c0ae24ccc500029915987",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 2,
"id": 62739,
"known_for_department": "Production",
"name": "Herb Gains",
"original_name": "Herb Gains",
"popularity": 5.403,
"profile_path": null,
"credit_id": "614c0b53326ec1008fcc8223",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 0,
"id": 2364439,
"known_for_department": "Production",
"name": "Joshua Grode",
"original_name": "Joshua Grode",
"popularity": 2.234,
"profile_path": null,
"credit_id": "614c0b66b04228002a111394",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 2,
"id": 54211,
"known_for_department": "Production",
"name": "Thomas Tull",
"original_name": "Thomas Tull",
"popularity": 3.825,
"profile_path": "/5UG4FK7rsmhzJDYxpU28acqfxtu.jpg",
"credit_id": "614c0b86cf4b8b004592546c",
"department": "Production",
"job": "Executive Producer"
},
{
"adult": false,
"gender": 2,
"id": 137427,
"known_for_department": "Directing",
"name": "Denis Villeneuve",
"original_name": "Denis Villeneuve",
"popularity": 38.749,
"profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
"credit_id": "6178ed77c2b9df0062239903",
"department": "Writing",
"job": "Screenplay"
},
{
"adult": false,
"gender": 2,
"id": 1322142,
"known_for_department": "Art",
"name": "Tom Brown",
"original_name": "Tom Brown",
"popularity": 3.99,
"profile_path": null,
"credit_id": "624075dbc740d9005d719d27",
"department": "Art",
"job": "Supervising Art Director"
},
{
"adult": false,
"gender": 0,
"id": 3482372,
"known_for_department": "Art",
"name": "Miklós Hatvani-Deàk",
"original_name": "Miklós Hatvani-Deàk",
"popularity": 2.157,
"profile_path": null,
"credit_id": "624075e545765d0047d4e7be",
"department": "Art",
"job": "Art Direction"
},
{
"adult": false,
"gender": 0,
"id": 3345289,
"known_for_department": "Art",
"name": "Adorjan Portik",
"original_name": "Adorjan Portik",
"popularity": 2.56,
"profile_path": null,
"credit_id": "624075f0e329430047e3aef4",
"department": "Art",
"job": "Art Direction"
}
]
const mergedData = data.reduce((acc, obj) => {
const existingObjIndex = acc.findIndex(item => item.name === obj.name);
if (existingObjIndex !== -1) {
acc[existingObjIndex].job.push(obj.job);
} else {
obj.job = [obj.job];
acc.push(obj);
}
return acc;
}, []);
| ready |