2016-07-03 2 views
0

데이터베이스 호출에서 만드는 자바 스크립트 개체가 있습니다. 여러 항목이있을 것이므로 특정 키/값 (moduleID)으로 참조 할 수 있어야합니다. 대신에 내가 무엇을 찾고 찾을 각각을 통해 루프를 갖는자바 스크립트 개체에서 데이터 찾기

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와 루프를 쓰기보다 더 간단합니다.

재미 있고 재미 있습니다!

관련 문제