2011-10-10 121 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次 –

相关问题