2016-10-27 12 views
-1

プロンプトが表示されたら、これをオブジェクトにループするにはどうすればよいですか? for...inループを使用して、オブジェクトのプロパティを超えるプロンプトが表示されたらオブジェクト内のオブジェクトをループする

var DennisLife = { 

     "greetings":{ 
      hello: " Hi there! ", 
      bye: "awe, leaving me already? ok, bye!", 
     }  
} 

var input = prompt("hello or bye").touppercase(); 
+0

プロンプト(DennisLife.greetings.hello)またはプロンプト(DennisLife。 greetings.bye) – juvian

答えて

0

あなたのループ、またはより近代的なブラウザでは、あなたはそれらの上にインデックスまたはループずつをつかむ配列にすべてのキーを取得するためにObject.keys()を使用することができます。

それとも、ユーザが入力したプロパティを取得しようとしている場合:

var input = prompt("hello or bye").toLowerCase(); 
alert(DennisLife.greetings[input]); 
0

あなたは

object.property 
object["property"] 
object[variable] // variable = 'property' 

var DennisLife = { 
 
     greetings: { 
 
      hello: " Hi there! ", 
 
      bye: "awe, leaving me already? ok, bye!", 
 
     }  
 
    }; 
 

 
var input = prompt("hello or bye").toLowerCase(); 
 

 
alert(DennisLife.greetings[input]);

のように、 property acessorとの直接アクセスを使用することができます
関連する問題