Ruby:将哈希的子数组一起添加

时间:2015-02-15 07:20:35

标签: ruby

我有

array_comb = {
  array1: [
    {url: 'https://www.youtube.com/channel/UCFR8mp0gz-h93DGGhsdLmrg', name: 'MAGIC SHOP FRENCH DROP'},
    {url: 'https://www.youtube.com/channel/UChKgsPL1MX5z_M33fYtOPKg', name: 'Ellusionist'}],
  array2: [
    {url: 'https://www.youtube.com/channel/UCfrhuhTDSpVhK7omh_ZIKaw', name: "Murphy's Magic"},
    {url: 'https://www.youtube.com/channel/UCEybzfsG2WI4Hnx5hpd2uRA', name: 'TheBlueCrownMagic'}],
  array3: [
    {url: 'https://www.youtube.com/channel/UCwlXbq5KtkdGC87oABDsfTQ', name: 'Dan & Dave'},
    {url: 'https://www.youtube.com/channel/UCl1WwbVpKUdQerivZpUxZLA', name: 'Theory 11'},]}

如何将3个子阵列组合成数组?

2 个答案:

答案 0 :(得分:2)

array_comb.values.flatten

values返回散列中所有值的数组。这最终成为一个数组数组。 flatten将所有数组合并为一个数组。

答案 1 :(得分:1)

另一种方式:

array_comb.flat_map(&:last)
  #=> [{:url=>"https:..."MAGIC SHOP FRENCH DROP"},
  #    {:url=>"https:..."Ellusionist"},
  #    {:url=>"https:..."Murphy's Magic"},
  #    {:url=>"https:..."TheBlueCrownMagic"},
  #    {:url=>"https:..."Dan & Dave"},
  #    {:url=>"https:..."Theory 11"}]