-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!

1
  • Did you try any code? Commented Aug 21, 2017 at 14:06

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

he/she has not included his attempt in the question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.