LoginSignup
0
0

[Ruby JavaScript] ハッシュ、オブジェクト比較

Posted at

目的

現在携わっているRails + Vue.jsのプロジェクトで、Ruby、JavaScriptそれぞれのキーと値を組み合わせてデータを管理する方法の呼び方、表記の仕方が混乱したので比較する。

Ruby ハッシュ

currencies = {'japan' => 'yen', 'us' => 'dollar', 'india' => 'rupee'}

# キーと値
# 値を取り出す
currencies['japan']

シンボルを使った場合

# キーのみシンボル
currencies = { japan: 'yen', us: 'dollar', india: 'rupee'}
currencies = { :japan => 'yen', :us => 'dollar', :india => 'rupee'}

# キーも値もシンボル
currencies = {japan: :yen, us: :dollar, india: :rupee}
currencies = { :japan => :yen, :us => :dollar, :india => :rupee}

# 値を取り出す
currencies[:japan]

JavaScript オブジェクト

var weather = { sunny: '晴れ', rain: '雨', cloudy: '曇り'}

// 値を取り出す
weather.sunny
weather['sunny']
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