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

发布于 2024-12-08 21:14:26 字数 1588 浏览 0 评论 0原文

您好,我有一个应用程序已经工作了一半。我有一个对象数组,每个对象的属性都已设置,并且可以像 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; //显示匹配数组中存储的伺服系统型号

我哪里出错了,为什么每当我尝试调用 matches[c].modelno 时总是收到“.modelno 为 null 或未定义”错误?

Hi I have an application that I have got half working. I have an array of objects, each with their properties already set and can call them like this myarray[i].property. I have an if statment that searchs through the array, within a loop, and pulls out any where myarray[i].property == my var.

The issue I'm having is that I want to put these results into a new array, built by the if statment/loop combo that searchs the first array, and I can't make it work.

This is what I have tried, but failed with?

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;}

Further in the code I have the line document.getElementById('servoDisplay').innerHTML = "search result " + matches[c].modelno; //display servos model numbers stored within the matches array

Where am I going wrong, why do I always get '.modelno is null or undefined' errors whenever I try to call matches[c].modelno?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独岁月 2024-12-15 21:14:26

让我试试。请告诉我我是否理解错误。我已将您的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 />');

Let me try. Please tell me if I understood you incorrectly. I have modifyed your JS code to the following:

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 />');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文