如何使用javascript循环创建对象数组
How do you create an array of objects using a javascript loop
嗨,我有一个应用程序,我有一半工作。我有一个对象数组,每个对象的属性都已经设置好了,可以把它们命名为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 />');
相关文章:
- 如何在jquery中从$.each循环创建对象数组
- 无法创建对象数组
- 创建对象数组的封装问题
- 下划线 从对象元素创建对象数组
- 读取 json 以使用 jQuery .each() 创建对象数组
- 基于文件名数据创建对象数组
- 按键/值在数组内创建对象数组
- 如何从字符串创建对象数组
- 如何从javascript中具有相同键的对象创建对象数组,对象中包含数组
- 如何从多个数组创建对象数组
- 如何在json中创建对象数组
- 在javascript中创建对象数组
- 根据id对对象进行分组,并创建对象数组
- 如何使用javascript循环创建对象数组
- 我如何在函数参数中创建对象数组,为每个对象调用函数
- 从数组中创建对象数组
- jQuery .each()创建对象数组
- 如何使用html dom为国家列表创建对象数组
- JavaScript / mongoose - 循环访问数组以创建对象数组
- 如果我们使用new array (len).fil({})创建对象数组;然后在任意一个对象中添加一个键,它会被反射到所有