2016-10-12 4 views
0

私は実際に答えを見つけるためにこの質問をする方法を知りませんでした。変数を持つオブジェクトに行く

は、だから私はそう

function gatherDataForGeographic(ele) { 
     var $this = $(ele) 
     var $main_title = $this.find('.options-title'), 
      $option = $this.find('.option'); 
     var arr = [] 
     var reportAreas = reportManager.getReportAreasObject(); 


     $option.each(function() { 
      var $this = $(this) 
      var $checkbox = $this.find('.checkbox'); 
      var type = $this.data('type'), 
       index = $this.data('index'); 

      if ($checkbox.hasClass('checkbox--checked')) { 
       console.log(reportAreas.type) 
      } else { 
       return true; 
      } 
     }) 
     return arr; 
    } 
    //this will return an object that I need to index 
    var reportAreas = reportManager.getReportAreasObject(); 

    //this will get the a key that i need from the object 
    var type = $this.data('type'); 

    //this will give me the index I need to grab 
    var index = $this.data('index'); 

に、この機能は、それが持っている何をするつもりされている3つの変数を持っているもの、私はからタイプ(またはキー)に基づいてオブジェクトを通過さやろうとしていますユーザーが選択したオプション

問題... reportArea.type[index]を探していて、変数として認識していません。タイプが存在しないため、undefinedを取得し続けます。

型が変数でありキーではないことを確認する方法はありますか?

答えて

1

あなたはブラケットの構文ではなく、ドット構文を使用してJSでの動的プロパティを使用することができます:reportAreas['whateverString']に解決されます

reportAreas[type] 

reportAreas.whateverStringと等価である - しかしreportAreas.typeは、typeプロパティのリテラルチェックです。

+0

完璧です、それは簡単でした。どうもありがとうございます!時間が許せば、これを答えとして受け入れます。 – Christian4423

1
reportArea[type][index] 

JavaScriptオブジェクトは単にキーと値のペアであり、ドットの構文は配列表記の構文上の砂糖です。

object['a'] 

object.a 

基本的には、同じものです。

関連する問題