如何将两个数组与1中的array2中的值合并?

时间:2021-11-13 18:01:19

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?

如何将两个数组与1中的array2中的值合并?

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

...

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

1 个解决方案

#1


4  

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 ]

#1


4  

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 ]