[英]How to merge values of a specific key with repeating objects into one object in an array of JSON object in Angular Typescript
I'm working with an array of JSON objects in Angular, which contains JSON objects with are repeated but differ in only one key-value pair, I want to merge the same value objects into one but append the values to the key which has different value: I'm working with an array of JSON objects in Angular, which contains JSON objects with are repeated but differ in only one key-value pair, I want to merge the same value objects into one but append the values to the key which has different价值:
Original Array:原始数组:
this.oldArray = [
{"song_id": 11, "artist_name": "Arijit", "song_name": "Song1"},
{"song_id": 11, "artist_name" : "Shreya", "song_name": "Song1"},
{"song_id": 12,"artist_name": "Jason", "song_name": "Song2"},
{"song_id": 12,"artist_name": "Ankit", "song_name": "Song2"},
{"song_id": 13,"artist_name": "Asha", "song_name": "Song3"},
{"song_id": 13,"artist_name": "Lata", "song_name": "Song3"},
{"song_id": 13,"artist_name": "Bapi", "song_name": "Song3"},
]
Desired Array:所需数组:
this.newArray = [
{"song_id": 11, "artist_name": "Arijit, Shreya", "song_name": "Song1"},
{"song_id": 12,"artist_name": "Jason, Ankit", "song_name": "Song2"},
{"song_id": 13,"artist_name": "Asha, Lata, Bapi", "song_name": "Song3"},
]
I tried using reduce, but could not achieve the desired output, kindly help me out我尝试使用reduce,但无法达到所需的output,请帮帮我
Thanks谢谢
This is one way to implement the reduce function.这是实现减少 function 的一种方法。 The principle is the following:
原理如下:
const oldArray = [ {"song_id": 11, "artist_name": "Arijit", "song_name": "Song1"}, {"song_id": 11, "artist_name": "Shreya", "song_name": "Song1"}, {"song_id": 12, "artist_name": "Jason", "song_name": "Song2"}, {"song_id": 12, "artist_name": "Ankit", "song_name": "Song2"}, {"song_id": 13, "artist_name": "Asha", "song_name": "Song3"}, {"song_id": 13, "artist_name": "Lata", "song_name": "Song3"}, {"song_id": 13, "artist_name": "Bapi", "song_name": "Song3"} ] const newArray = oldArray.reduce( (acc, song) => { const foundIndex = acc.findIndex(({ song_id }) => song_id === song.song_id) return foundIndex === -1 // Song not found, simply add it? [...acc, song] // Song is found, edit it with the artist: [ // Copy existing list until the found song...acc.slice(0, foundIndex), // edit found song with the new artist {...acc[foundIndex], artist_name: `${acc[foundIndex].artist_name}, ${song.artist_name}` }, // Copy the rest of the existing list after the found song...acc.slice(foundIndex + 1, acc.length) ] }, [] ) console.log(newArray)
Use uniq
from lodash.使用 lodash 中的
uniq
。
const oldArray = [
{"song_id": 11, "artist_name": "Arijit", "song_name": "Song1"},
{"song_id": 11, "artist_name": "Shreya", "song_name": "Song1"},
{"song_id": 12, "artist_name": "Jason", "song_name": "Song2"},
{"song_id": 12, "artist_name": "Ankit", "song_name": "Song2"},
{"song_id": 13, "artist_name": "Asha", "song_name": "Song3"},
{"song_id": 13, "artist_name": "Lata", "song_name": "Song3"},
{"song_id": 13, "artist_name": "Bapi", "song_name": "Song3"}
]
const newArray = _.uniq(oldArray, function (e) {
return e.song_id;
});
console.log(newArray)
Evidence证据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.