2016-07-03 13 views
0

私はデータベース呼び出しから作成しているjavascriptオブジェクトを持っています。複数の項目があり、特定のキー/値(moduleID)で参照できるようにする必要があります。代わりに私が探しています何を見つけるために、それぞれ1以上のループを有するのJavascriptオブジェクトでデータを見つける

success: function(data) { 

     // Define our local vars 
     var moduleIncludes = Array(); 

     // Loop over each of the users selected modules and put them into an array 
     $(data).find('modules').each(function() { 

      // Push module JS file names to an array 
      moduleIncludes.push($(this).find('moduleJSFile').text()); 

      // Create an object of module data 
      moduleData.push({ 
       moduleID: $(this).find('moduleID').text(), 
       moduleRow: $(this).find('row').text(), 
       moduleColumn: $(this).find('col').text(), 
       moduleName: $(this).find('moduleName').text(), 
       moduleDescription: $(this).find('moduleDescription').text(), 
       moduleJSFile: $(this).find('moduleJSFile').text(), 
       moduleIcon: $(this).find('moduleIcon').text(), 
       moduleStartingXsize: $(this).find('startingXsize').text(), 
       moduleStartingYsize: $(this).find('startingYsize').text(), 
       moduleResizeLocked: $(this).find('resizeLocked').text(), 
       moduleMinXsize: $(this).find('minXsize').text(), 
       moduleMinYsize: $(this).find('minYsize').text() 
      }); 
     }); 

     // Using our array of modules, fetch them to include them into the page 
     $.getMultiScripts(moduleIncludes, 'includes/js/modules/').done(function() { 
      // All of the modules have been included. Trigger the grid functionality. 
      renderCanvas(); 
     }); 
    } 
}); 

、私はそのユニークなので、代わりにmoduleIDことで、オブジェクトに名前を付けることができます方法はありますか?

たとえば、moduleId 1に関連するすべてのデータを表示したい場合は、それをループするのではなく、IDでオブジェクトを検索するだけです。

私は作成時にこれを達成できますか?

enter image description here

+0

「データ」の形式の例を表示できますか?あなたが配列を持っているなら、それをループするか( '.find()'や '.filter()')を使う必要がありますが、オブジェクトがあれば、キー名でプロパティを参照できます。 – nnnnnn

+0

データは、ストアドプロシージャから渡されたXML文字列です。オブジェクトはイメージで見られるように細かく作成されていますが、IDで何らかの形で定義する必要があります。 – SBB

答えて

1

多分あなたが必要とするものよりのようなものです:

このようにすること
moduleIncludes = {}; 
moduleIncludes[Id] = {"moduleRow":$(this).find('row').text() ...}; 

、あなたはIDから直接、特定のモジュールを要求することができるでしょう。

+0

これは私が必要なものです。今私はmoduleIncludes [1] .moduleRowと言うことができます – SBB

0

filter()機能は、オブジェクトの検索に使用できます。

文書w3schools

例:オブジェクトID

function matchId(id) { 
    return moduleData.moduleID == id; 
} 

data = moduleData.filter(matchId); 

によるフィルタは、あなたがforまたはforeachでループを書くよりも簡単です。

Goodluckと楽しい!

関連する問題