如何合并同一关联数组中相同键对应的值以生成JSON?
Merge duplicate category entries by combining their video objects in a JSON array
Hey there! I totally get the frustration of dealing with duplicate category entries in your JSON array—you want to group those together instead of having repeated categories cluttering up your data. Let's walk through how to fix this step by step.
Original JSON Data
{ "trailervideos": [ { "category": "Video Games", "videos": { "description": "Trailer of the game Dark Souls II", "title": "Dark Souls 2 Announcement Trailer" } }, { "category": "Video Games", "videos": { "description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II", "title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer" } }, { "category": "Video Games", "videos": { "description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III", "title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4" } }, { "category": "Entertainment", "videos": { "description": "", "title": "intro" } } ] }
Desired Result
{ "trailervideos": [ { "category": "Video Games", "videos": [ { "description": "Trailer of the game Dark Souls II", "title": "Dark Souls 2 Announcement Trailer" }, { "description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II", "title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer" }, { "description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III", "title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4" } ] }, { "category": "Entertainment", "videos": { "description": "", "title": "intro" } } ] }
Solution (JavaScript)
We'll use a helper object to group entries by their category key, then convert that grouped data back into your desired array format. Here's the code:
// Parse your original JSON into a JS object (skip this if you already have it as an object) const originalData = JSON.parse(`{ "trailervideos": [ { "category": "Video Games", "videos": { "description": "Trailer of the game Dark Souls II", "title": "Dark Souls 2 Announcement Trailer" } }, { "category": "Video Games", "videos": { "description": "Trailer of the DLC Scholar of the First Sin for the game Dark Souls II", "title": "Dark Souls II Scholar of the First Sin - Forlorn Hope Trailer" } }, { "category": "Video Games", "videos": { "description": "Trailer of the DLC Ashes of Ariendel for the game Dark Souls III", "title": "Dark Souls III Ashes of Ariandel - Announcement Trailer PS4" } }, { "category": "Entertainment", "videos": { "description": "", "title": "intro" } } ] }`); // Helper object to group entries by category const categoryGroups = {}; // Loop through each entry and build our grouped data originalData.trailervideos.forEach(entry => { const category = entry.category; // Initialize the category entry if it doesn't exist if (!categoryGroups[category]) { categoryGroups[category] = { category: category, videos: [] }; } // Add the video object to the category's videos array categoryGroups[category].videos.push(entry.videos); }); // Convert the grouped object back into an array, and adjust single-video entries const mergedData = { trailervideos: Object.values(categoryGroups).map(item => { // If only one video exists, keep it as an object (matches your desired output) if (item.videos.length === 1) { item.videos = item.videos[0]; } return item; }) }; // Output the merged result (convert back to JSON if needed) console.log(JSON.stringify(mergedData, null, 2));
Quick Breakdown
- Grouping by Category: We iterate over each entry and use a helper object to track each category. If a category isn't already in the helper, we set it up with an empty
videosarray. - Merging Videos: Every video object gets pushed into its category's
videosarray. - Final Formatting: We convert the helper object into an array. For categories with only one video, we convert the
videosarray back to a single object to match your desired output.
If you're working in another language, the core logic stays the same: use a dictionary/map to group entries by category, then combine the videos values for each group.
内容的提问来源于stack exchange,提问作者user3682205
相关产品推荐
相关产品推荐

