简体   繁体   English

How to merge values of a specific key with repeating objects into one object in an array of JSON object in Angular Typescript

[英]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:原理如下:

  1. search if the song already exists in the accumulator array.搜索歌曲是否已经存在于累加器数组中。
  2. if so, concat the artist to the list如果是这样,将艺术家连接到列表中
  3. if not, simply copy the song record in the accumulator array如果没有,只需将歌曲记录复制到累加器数组中

 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.

相关问题 如何连接对象数组中的值-对象中的特定键 - How to connect values in array of objects - a specific key in the object 如何合并两个包含相同键的不同值的对象数组,并将新的 object 推入数组? - How to merge two array of objects containing different values of same key, and also push new object into the array? 如何通过一键过滤json数组中的对象 - how filter object in json array by one key 如何在Angular Typescript中的对象数组中提取对象的某些字段 - How to extract certain fields of an object in an array of objects in Angular Typescript 如何在 typescript (Angular) 中数组的特定索引处向 object 添加属性? - How to add a property to an object at a specific index of an array in typescript (Angular)? 如何在 Angular(打字稿)中的 JSON object 中添加新的键数组? - How to add a new array of keys to a JSON object in Angular (typescript)? 从 Typescript angular 中的 json object 获取数组 - Fetching array from json object in Typescript angular 如何在打字稿中动态合并两个数组对象? - How to merge two array object dynamically in typescript? 使用angular和Typescript获取对象值数组 - Fetch array of object values using angular and typescript 如何使用angular 8从json数组中获取特定的json对象 - how to get specific json object from json array using angular 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM