如何使用 javascript 循环创建对象数组
您好,我有一个应用程序已经工作了一半。我有一个对象数组,每个对象的属性都已设置,并且可以像 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我试试。请告诉我我是否理解错误。我已将您的JS代码修改为以下内容:
Let me try. Please tell me if I understood you incorrectly. I have modifyed your JS code to the following: