2011-10-10 106 views
-1

嗨我有一個應用程序,我有一半的工作。我有一組對象,每個對象的屬性已經設置好,可以像這樣調用它們myarray[i].property。我有一個if語句,可以在循環內搜索數組,並在任何地方提取出myarray[i].property == my var如何使用JavaScript循環創建一個對象數組

我遇到的問題是,我想把這些結果放入一個新的數組中,該數組由搜索第一個數組的if語句/循環組合構建,並且我無法使其工作。

這是我試過的,但失敗了?

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或undefined」錯誤,每當我打電話比賽[C]。型號?

+2

我假設'mew'實際上是'new'? –

+0

y只是一個錯字,因爲我沒有複製和粘貼代碼,因爲它在這個不同的電腦 –

+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

這就是我想要的歡呼Lappie接近我希望存儲每個屬性,newEntry有一個數組像「 appRes「,因爲我的項目的下一個階段是搜索」appRes「數組並重復該過程,這次將所有匹配放入一個名爲」classRes「的新數組中 我試圖創建的應用程序是一個搜索過濾器,我有6個框來過濾搜索,所以我想如果statement1 = condition1然後用結果構建數組,然後在condtion2中搜索該數組,然後構建新的數組,結果總共重複6次 –

相關問題