简体   繁体   中英

how to combine 2 different array lists [Angular,typescript]

I have 2 arraylist as below where I have to combine the array to get all the values in the new array list where the uuid will be common in both of the lists, the final list should have all the values associated witht he UUID in the both arraylist

list 1:

[
  {
  "uuid":"01",
  "plan" : "aaaa",
  "other_details":"xxxxxx"
  },
  {
    "uuid" : "02",
    "plan" : "bbbb",
    "other_details":"yyyyyyy"
  },
  {
    "uuid" : "03",
    "plan" : "cccc",
    "other_details":"zzzzzz"
  },
  {
    "uuid" : "04",
    "plan" : "dddd",
    "other_details":"uuuuuu"
  }
]

list 2:

[
  {
  "uuid":"01",
  "office" : "India",
  "status":"running"
  },
  {
    "uuid" : "02",
    "office" : "USA",
    "status":"running"
  },
  {
    "uuid" : "03",
    "office" : "Germany",
    "status":"running"
  },
  {
    "uuid" : "04",
    "office" : "Australia",
    "status":"shutdown"
  }
]

I have to combine 2 array lists with unique "UUID", the array should look like

[
  {
  "uuid":"01",
  "plan" : "aaaa",
  "other_details":"xxxxxx",
    "office" : "India",
  "status":"running"
  },
  {
    "uuid" : "02",
    "plan" : "bbbb",
    "other_details":"yyyyyyy",
    "office" : "USA",
    "status":"running"
  },
  {
    "uuid" : "03",
    "plan" : "cccc",
    "other_details":"zzzzzz",
    "office" : "Germany",
    "status":"running"
  },
  {
    "uuid" : "04",
    "plan" : "dddd",
    "other_details":"uuuuuu",
    "office" : "Australia",
    "status":"shutdown"
  }
]

can I iterate the array as below?

   for(listItem1 in arraylist1){  //assuming I am storing the arraylist in variable arraylist1
     for (listitem2 in arraylist2){
    if(listItem1.uuid=== listItem2.uuid){
listItem = "" //assign values
}
    }
    }

how to create the 3rd array list and store the items in the arraylist

Try this; used spread operator of ECMAScript to simplify it.

const mergedList = listOne.map(listItem1 => ({...listItem1, ...listTwo.find(listItem2 => listItem2.id === listItem1.id)}))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM