0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

配列の中にハッシュがある場合の取り出し

Posted at

配列の中にハッシュ

以下の配列からnameの値を取り出して取得する

user_data = [
  {
    user: {
        profile: {
            name: 'Tanaka'
        }
    }
  },
  {
    user: {
        profile: {
          name: 'Sato'
        }
    }
  },
  {
    user: {
        profile: {
            name: 'Saito'
        }
    }
  }
]

user_data.each do |u|
  puts u[:user][:profile][:name]
end

eachでuに値を入れて出力

user_data.each { |u| puts u.dig(:user, profile, name)}

でも出力することができる
digメソッドは引数で渡したキーから値を取り出すことができる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?