0

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

1
  • How about listItem = { ...listItem1, ...listItem2 }? Commented May 31, 2021 at 5:32

1 Answer 1

4

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

const mergedList = listOne.map(listItem1 => ({...listItem1, ...listTwo.find(listItem2 => listItem2.id === listItem1.id)}))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.