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

How do you create an array of objects using a javascript loop

本文关键字:创建对象 数组 循环 javascript 何使用      更新时间:2023-09-26

嗨,我有一个应用程序,我有一半工作。我有一个对象数组,每个对象的属性都已经设置好了,可以把它们命名为myarray[i].property。我有一个if语句,在一个循环中搜索数组,并取出任何位置的myarray[i].property == my var

我遇到的问题是,我想把这些结果放入一个新的数组,由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或未定义的错误,每当我试图调用匹配[c].modelno?

让我试试。如果我理解错了,请告诉我。我已经修改了你的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 />');