0

I have 3 arrays

Array1 = ["u101", "u102", "u103", "p101"];

Array2 = ["u101", "u102", "u103", "u104"];

Array3 = ["p101"]

after comparing Array1 and Array2 I need to push "u104" into Array1 and remove "p101" by comparing it with Array3 dynamically.

expected final array would be

newArray = ["u101", "u102", "u103", "u104"];

Can this be achieved with single line of code using ES6 verson of javascript.

All other similar questions are like how to get the unmatched elements or matched elements after comparing two arrays. But here, I need to push those unmatched elements into one of the arrays.

Thank you.

Gowtham Manthena
  • 199
  • 5
  • 21
  • So your goal is to pick the same items from `Array1` and `Array2` and then remove any items that are in `Array3`? – deaponn Aug 18 '22 at 16:49
  • my goal is to compare array1 and array2 and push the unmatched element of array2 into array1 after that I again needs to compare the output array with array3 and remove the unmatched element from array 3 – Gowtham Manthena Aug 18 '22 at 16:54

1 Answers1

2

Use this bit of code

const result = [
        ...Array1, 
        ...Array2.filter(item => !Array1.includes(item))
    ].filter(item => !Array3.includes(item))
deaponn
  • 837
  • 2
  • 12