I'm trying to create an array of objects in JavaScript and fill each from an XML files. The XML file has already been loaded in a previous function. However when i try to push the new product i get an error in console that x[i] is undefined.
My code is:
function CreateProductObject() {
function productObject(name, brand, price, info, material, photo) {
this.name = name;
this.brand = brand;
this.price = price;
this.info = material;
this.photo = photo;
}
var productArray = [];
var numOfProducts = CountProducts(productType);
console.log(productType + " needs to be Displayed: " + numOfProducts);
x = xmlDoc.getElementsByTagName(productType);
for (i = 0; i <= numOfProducts; i++) {
productArray.push(
new productObject(
x[i].getElementsByTagName("NAME")[0].childNodes[0].nodeValue,
x[i].getElementsByTagName("BRAND")[0].childNodes[0].nodeValue,
x[i].getElementsByTagName("PRICE")[0].childNodes[0].nodeValue,
x[i].getElementsByTagName("INFO")[0].childNodes[0].nodeValue,
x[i].getElementsByTagName("MATERIAL")[0].childNodes[0].nodeValue,
x[i].getElementsByTagName("PHOTO")[0].childNodes[0].nodeValue,
),
);
console.log(productArray[i].name);
}
}
Any help appreciated.
您尚未声明x
,因此它是未定义的。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.