如何使用javascript循环创建对象数组

时间:2011-10-10 08:41:01

标签: javascript arrays object loops

嗨,我有一个应用程序,我有一半工作。我有一个对象数组,每个对象都已经设置了属性,可以像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或undefined'错误[c] .modelno?

1 个答案:

答案 0 :(得分: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 />');