Create an array of unique values found shared among multiple given arrays?

Answered
timmy123 asked this question 4 months ago
timmy123 on Jan 19

When coding in my JavaScript project, I have multiple arrays that look like below:

[1, 32, 142, 102, 43, 49]
[32, 321, 49, 8, 1]
[4, 1, 49, 32]

How do I take those arrays and create a new array that is present in each of the given arrays?

Given the example arrays above, it should return this:

[1, 49, 32]

Those values are found in each of the three arrays.

Thanks in advance for the help!

1 suggested answers
suparman21 on Jan 20

Lodash has a intersection() method you can use like this:

_.intersection(...Array)

You'd use it in your example like this:

_.intersection([1, 32, 142, 102, 43, 49], [32, 321, 49, 8, 1], [4, 1, 49, 32])

// returns [1, 32, 49]
0 replies
Answered