Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何从另一个数组中的多个元素中选择数组中的元素_Arrays_Json_Ruby_Object - Fatal编程技术网

Arrays 如何从另一个数组中的多个元素中选择数组中的元素

Arrays 如何从另一个数组中的多个元素中选择数组中的元素,arrays,json,ruby,object,Arrays,Json,Ruby,Object,我有一个数组A,看起来像这样: A = [ { "id" => "1234", "name" => "audi", "isCool" => false }, { "id" => "5678", "name" => "acura", "isCool" => false }, { "id" => "9101112", "name" => "bentley", "isCool" =&g

我有一个数组
A
,看起来像这样:

A = [ { "id" => "1234",     "name" => "audi",       "isCool" => false },
      { "id" => "5678",     "name" => "acura",      "isCool" => false },
      { "id" => "9101112",  "name" => "bentley",    "isCool" => true  },
      { "id" => "13141516", "name" => "rollsroyce", "isCool" => true  },
      { "id" => "17181920", "name" => "toyota",     "isCool" => true  } ]
B = ["1234", "13141516”]
我有一个数组
B
,看起来像这样:

A = [ { "id" => "1234",     "name" => "audi",       "isCool" => false },
      { "id" => "5678",     "name" => "acura",      "isCool" => false },
      { "id" => "9101112",  "name" => "bentley",    "isCool" => true  },
      { "id" => "13141516", "name" => "rollsroyce", "isCool" => true  },
      { "id" => "17181920", "name" => "toyota",     "isCool" => true  } ]
B = ["1234", "13141516”]
我试图从数组A中只选择与数组A的ID和数组Bs元素匹配的元素

因此,我希望返回的结果是:

C = [ { "id" => "1234",     "name" => "audi",       "isCool" => false },
      { "id" => "13141516", "name" => "rollsroyce", "isCool" => true  } ]
有没有一个简单的方法

我目前尝试过这个方法,但显然不是一个好主意:

a.select {|x| x['id'] == B.first || B.last}
但显然这不是动态的,因为如果数组
B
中有3或4个元素会怎么样

A.select { |x| B.include?(x['id']) }