2011-10-10 3 views
-1

안녕하세요, 저는 반쪽으로 작동하는 응용 프로그램이 있습니다. 각 개체의 속성이 이미 설정된 배열이 있으며이 개체를 myarray[i].property과 같이 호출 할 수 있습니다. 루프 내에서 배열을 검색하는 if 문을 가지고 있으며 어디에서든지 코드를 추출합니다. myarray[i].property == my var.어떻게하면 자바 스크립트 루프를 사용하여 객체의 배열을 만들 수 있습니까

내가 겪고있는 문제는 첫 번째 배열을 검색하는 if statment/loop 콤보에 의해 작성된 새로운 배열에 이러한 결과를 넣고 싶습니다. 작동하지 못하는 것입니다.

이것은 내가 시도했지만 실패한 것입니까?

var c = 0; 
var matches = new Array('application', 'sclass', 'type', 'motor', 'bearings', 'gears', 'modelno', 'name', 'speed', 'v3_3', 'v4_8', 'v6_0', 'v7_2', 'weight', 'diensions', 'opvoltage', 'image', 'description'); 

//loop through servos array and pull any servo that has a matching application value to that selected by the search filter 
for(var i=0; i < servos.length; i++){ 
    if servos[i].application == document.searchFilters.applicationMenu.value) { 
     //populate the new 'matches' array with the details from the servos pulled from the inital arary 
     matches[c] = new servo(servos[i].application, servos[i].sclass, servos[i].type, servos[i].motor, servos[i].bearings, servos[i].gears, servos[i].modelno, servos[i].name, servos[i].speed, servos[i].v3_3, servos[i].v4_8, servos[i].v6_0, servos[i].v7_2, servos[i].weight, servos[i].dimensions, servos[i].opvoltage, servos[i].image, servos[i].description); 
     c++; 
     } else if (document.searchFilters.applicationMenu.value == 0){ 
     //sets the value of servoDtore locally 
     var servoStore = 0;} 

은 또한 코드에서 내가 줄을 내가 잘못 가고 document.getElementById('servoDisplay').innerHTML = "search result " + matches[c].modelno; //display servos model numbers stored within the matches array

이 왜 난 항상받을 수 있나요 '.modelno null 또는 정의되지 않은'내가 경기를 호출 할 때마다 오류 [C]. 모델 번호?

+2

'mew'는 실제로'new'라고 가정합니까? –

+0

예전에 오타가 오타되었으므로 코드를 복사하여 붙여 넣지 않았습니다.이 PC와는 다른 PC에 있습니다. –

+0

C 값을 다시 설정합니까? ...? – geekchic

답변

1

시험해 봅니다. 내가 너를 잘못 이해했는지 말해줘. JS 코드를 다음과 같이 수정했습니다 :

var matches = ['application', 'sclass', 'type', 'motor', 
       'bearings', 'gears', 'modelno', 'name', 'speed', 
       'v3_3', 'v4_8', 'v6_0', 'v7_2', 'weight', 
       'dimensions', 'opvoltage', 'image', 'description'], 
    output = [], 
    modelnos = []; 
    // c variable is unnecessary now 

// Loop through servos array and pull any servo that has a matching application value to that selected by the search filter 
for(var i = 0, len = servos.length; i < len; i+= 1) { 
    if (document.searchFilters.applicationMenu.value === servos[i].application) { 
     // Populate the new 'matches' array with the details from the servos pulled from the inital arary 
     var newEntry = new servo(servos[i].application, servos[i].sclass, servos[i].type, servos[i].motor, 
           servos[i].bearings, servos[i].gears, servos[i].modelno, servos[i].name, servos[i].speed, 
           servos[i].v3_3, servos[i].v4_8, servos[i].v6_0, servos[i].v7_2, servos[i].weight, 
           servos[i].dimensions, servos[i].opvoltage, servos[i].image, servos[i].description); 

     output.push(newEntry); 
     modelnos.push(newEntry.modelno); 
     // c++; 
    } else if (document.searchFilters.applicationMenu.value === 0) { 
     var servoStore = 0; 
    } 
} 

// Display servos model numbers stored within the matches array 
document.getElementById('servoDisplay').innerHTML = "Search result: " + modelnos.join('<br />'); 
+0

그게 내가 가까이 갈채를 원해요 근처에있다. 나는 newEntry가 하나의 배열 내에 " appRes "내 프로젝트의 다음 단계는"appRes "배열을 검색하고 프로세스를 반복하는 것이므로 이번에는"classRes "라는 새로운 배열에 일치 항목을 넣으려고합니다. 내가 만들려고하는 앱은 검색 필터입니다 , 여섯 상자에 의해 검색을 필터링 할 수 있습니다. 그래서 내가 원하는 경우 statement1 = condition1 그런 다음 결과 배열, 다음 해당 배열을 condtion2 검색 및 해당 결과를 가진 새 배열을 총 6 회 반복 –

관련 문제