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 5 years have passed since last update.

phpやってた人が javascript 多次連想配列をループする 値を取得する

Last updated at Posted at 2016-10-17

以下の多次連想配列があったとして

var fruits = {
   Apple:{color:red, shape:round},
   Grape:{color:purple, shape:grain},
   Peach:{color:pink, shape:heart}
   };

ループして取得する

for(var key in fruits){
var value = fruits[key];
console.log("フルーツの種類:" + key); //出力:連想配列fruitsのキーAppleなど

   for(var key2 in value){
   var value2 = value[key2];
   }

   console.log("色は" + value.color); //連想配列valueのキーcolorの値
   console.log("形は" + value.shape); //連想配列valueのキーshapeの値
}
出力
フルーツの種類:Apple
色はred
形はround
フルーツの種類:Grape
色はpurple
形はgrain
フルーツの種類:Peach
色はpink
形はheart

って地道にやってしまったのだが、スマートにやるには

ループして取得する

for(var key in fruits){
console.log("フルーツの種類:" + key); 
console.log("色は" + fruits[key].color); 
console.log("形は" + fruits[key].shape); 
}

だけでよかったという、、

0
0
3

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?