按特定条件删除数组元素

m.s*_*nus 8 ruby arrays

什么是最好的方法:我有两个数组:

a=[['a','one'],['b','two'],['c','three'],['d','four']]
Run Code Online (Sandbox Code Playgroud)

b=['two','three']

我想删除a包含元素的嵌套数组b,以获得:

[['a','one']['d','four']
Run Code Online (Sandbox Code Playgroud)

谢谢.

Sim*_*tti 17

a = [['a','one'],['b','two'],['c','three'],['d','four']]
b = ['two','three']

a.delete_if { |x| b.include?(x.last) }

p a
# => [["a", "one"], ["d", "four"]]
Run Code Online (Sandbox Code Playgroud)


ste*_*lag 5

rassoc救援!

 b.each {|el| a.delete(a.rassoc(el)) }
Run Code Online (Sandbox Code Playgroud)