-2

Let's take two arrays for example:

a = [1, 2, 3, 4, 5]
b = [4, 5, 6, 7, 8]

Now there is duplicates as we see 4 and 5. How to make new array from them instead of getting rid of them. What is the easiest solution? So new array should be like:

newArray = [4, 5]

Thank you guys in advance!

litelite
  • 2,857
  • 4
  • 23
  • 33

1 Answers1

0

You can do it using Array.filter() and Array.includes()

let a = [1, 2, 3, 4, 5];
let b = [4, 5, 6, 7, 8];


let arr = a.filter(function(x){
    return b.includes(x);
})

console.log(arr);
marvel308
  • 10,288
  • 1
  • 21
  • 32