tonymarschall
tonymarschall

Reputation: 3882

Howto merge two arrays with values from array2 that are in 1?

I have an array with allowed values and an array with given values.

Howto merge two arrays with values from array2 that are in 1?

allowed_values => ["one", "two", "three"]
given_values => ["", "one", "five", "three", "seven"]

...

expected_values => ["one", "three"]

Upvotes: 2

Views: 119

Answers (1)

user229044
user229044

Reputation: 239311

You want the array intersection, and you can obtain it via the & operator:

Set Intersection—Returns a new array containing elements common to the two arrays, with no duplicates.

[ 1, 1, 3, 5 ] & [ 1, 2, 3 ]   #=> [ 1, 3 ]

Upvotes: 4

Related Questions