简体   繁体   English

迭代多维数组以获取特定值 ruby

[英]iterating over a multidimensional array to grab specific values ruby

I'm using the Bandsintown api to grab upcoming concert dates based on which artists a user is following on my site and where the user is located.我正在使用 Bandsintown api 根据用户在我的网站上关注的艺术家以及用户所在的位置来获取即将举行的音乐会日期。 Right now I'm having trouble iterating over a multidimensional array of data that I get back as a response.现在我在迭代作为响应返回的多维数据数组时遇到了麻烦。

The Goal目标

To iterate through an array of arrays and then grab an artists' name to display it on my view.遍历数组数组,然后获取艺术家的姓名以将其显示在我的视图中。

I'm able to grab the data I want with the following code inside my controller:我可以使用控制器中的以下代码获取我想要的数据:

class CalendarController < ApplicationController
  require 'uri'

  def index
    @user = current_user
    @hash_version_array = []

    @user.follows.each do |follow|
      response = HTTParty.get("http://api.bandsintown.com/artists/#{URI.escape(follow.artist_name)}/events/search.json?api_version=2.0&app_id=el_proyecto_de_la_musica&location=use_geoip")
      @hash_version = JSON.parse(response.body)

      @hash_version_array << @hash_version
    end

    @hash_version_array
  end

  def show
  end
end

the code above produces the following results inside of my @hash_version_array (click here for the results, easier to show through a gist on github)上面的代码在我的 @hash_version_array 中产生以下结果(点击这里查看结果,更容易通过 github 上的要点显示)

I get stuck when trying to iterate over @hash_version_array to grab each artist's name.我在尝试遍历 @hash_version_array 以获取每个艺术家的名字时卡住了。

Attempt 1尝试 1

[9] pry(#<CalendarController>)> @hash_version_array.each do |sub_array|
[9] pry(#<CalendarController>)*   sub_array.each do |artist|  
[9] pry(#<CalendarController>)*     artist["artists"]["name"]    
[9] pry(#<CalendarController>)*   end  
[9] pry(#<CalendarController>)* end

Results in结果是

TypeError: no implicit conversion of String into Integer
from (pry):96:in `[]'

Attempt 2尝试 2

[8] pry(#<CalendarController>)> @hash_version_array.each do |sub_array|
[8] pry(#<CalendarController>)*   sub_array.each do |artist|
[8] pry(#<CalendarController>)*     artist["artists"].each do |artist|
[8] pry(#<CalendarController>)*       artist["name"]
[8] pry(#<CalendarController>)*     end  
[8] pry(#<CalendarController>)*   end  
[8] pry(#<CalendarController>)* end

Results in the same value I got for @hash_version_array to begin with结果与我为@hash_version_array 获得的值相同

To get the name of an artist, you'll have to fetch it via the following structure:要获取艺术家的姓名,您必须通过以下结构获取它:

artist[0]["artists"][0]["name"]

I'll leave it up to you to make the code more robust.我会把它留给你来使代码更健壮。

A Hash is a dictionary-like collection of unique keys and their values哈希是一个类似字典的唯一键及其值的集合

hash = {my_key: my_value}

so if you want to get a value just use hash['my_key'] docs所以如果你想获得一个value只需使用hash['my_key'] docs

  hash.each do |key,value|
      key['my_key']
    end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM