【问题标题】:How to join two table hash in ruby如何在 ruby​​ 中加入两个表哈希
【发布时间】:2018-12-02 10:16:17
【问题描述】:

我正在训练 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"], 
....
    ]

如果你可以的话,这将是非常有帮助的,因为桌子对我来说很难,谢谢

【问题讨论】:

  • 这似乎是一个纯 Ruby 问题,在这种情况下你不应该有“Sinatra”标签。

标签: arrays ruby


【解决方案1】:

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

您可以使用Array#zipArray#flatten 获得所需的结果。

flatten 接受一个可选参数,表示它应该展平的嵌套数组的数量。

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

【讨论】:

  • 嗨,卡里,好久不见!您可以通过连接上述方法得到答案。 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 2017-12-18
  • 1970-01-01
  • 2016-02-27
  • 2021-05-03
  • 1970-01-01
  • 2013-05-20
相关资源
最近更新 更多