如何在Ruby中加入两个表哈希

时间:2018-12-02 10:16:17

标签: arrays ruby

我正在训练红宝石,我需要做运动,但是我不知道如何获取数据 我有这个

DB = [
  {
    title: "My first article",
    content: "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id ratione harum illo, dicta minima rerum quod natus cupiditate voluptatibus rem! Amet reprehenderit voluptatum animi, eligendi quia quos reiciendis veritatis magni.",
    photo: "https://google.fr",
    rating: "2"
  },
  {
    title: "An second article",
    content: "Eventually the guys with real ideas not running a scam on the world will overcome. I used an exclamation just to be an asshole and also to make a point! LOL Young Thug is super inspiring to me. Amazing artist!!! I also wanted to point out that it’s the first album to go number 1 off of streaming!!! ",
    photo: "https://google.fr",
    rating: "2"
  },
  {
    title: "3 article",
    content: "also to Kim’s hair and makeup Lorraine jewelry and the whole style squad at Balmain and the Yeezy team. When companies doubt me they doubt us. Wes daytime",
    rating: "4"
  },
]


COMMENTS = [
  ["Hello1"],
  ["comment2"],
  ["comment3"],
]

我想要这个

 DATAS = [ {
        title: "My first article",
        content: "Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id ratione harum illo, dicta minima rerum quod natus cupiditate voluptatibus rem! Amet reprehenderit voluptatum animi, eligendi quia quos reiciendis veritatis magni.",
        photo: "https://google.fr",
        rating: "2"
      }, ["Hello1"], 
....
    ]

如果可以的话,那将是非常痛苦的,因为对我来说餐桌很难,谢谢

1 个答案:

答案 0 :(得分:0)

您没有编写任何代码,所以我不会给您完整的答案。

您可以使用Array#zipArray#flatten获得理想的结果。

flatten接受一个可选参数,以指定应展平的嵌套数组的数量。

因此DB.zip(COMMENTS).flatten几乎就是您想要的。您只需要向flatten提供合适的参数即可。