DEV Community

elijah4000
elijah4000

Posted on

Looping through an array and linking to html

I'm trying to loop through an array of car data and create an array of Car objects using a forEach loop. Then, I want to display each car in the appropriate section of the page depending on whether it is "safetied" or not.

Here’s my Car class:

js

class Car {
constructor(manufacturer, model, year, vin, safetied) {
this.manufacturer = manufacturer;
this.model = model;
this.year = year;
this.vin = vin;
this.safetied = safetied;
}

get outputString() {
    return `${this.manufacturer} ${this.model} ${this.year} with ${this.vin}`;
}
Enter fullscreen mode Exit fullscreen mode

}
My car data is in a separate file, cardata.js, and looks like this:

js

let carData = [];
carData.push(["Cadillac","DeVille",1993,"1FTEW1CM7CK070304",true]);
// ... and many more entries in this same array format
Here’s the logic I originally used in parta.js:

js

let cars = [];

carData.forEach(car => {
const newCar = new Car(car.manufacturer, car.model, car.year, car.vin, car.safetied);
cars.push(newCar);
});
But this gave me undefined values or didn't work as expected.

Top comments (0)