I'm struggling to understand if my issue lies in how I am organizing my array or in how I am accessing it.
The idea is simple: I want to have an array of car makes and models that I can access for display purposes.
const carBrands = [
{
Audi: {
models: ["A1", "A2", "A3", "A4"],
unitsAvailable: 378,
}
},
{
BMW: {
models: ["M1", "M2", "M3", "M4"],
unitsAvailable: 413,
},
},
{
Honda: {
models: ["H1", "H2", "H3", "H4"],
unitsAvailable: 226,
},
}
];
I am able to access each model like this:
carBrands.forEach((object, i) => {
console.log(object, i)
});
This gives me the objects and their index as expected:
Audi 0 BMW 1 Honda 2
What I am struggling with is how to display:
Make: Audi Models: A1, A2, A3, A4 Units available: 378
... for each of the 3 cars.
I know I need to loop over the array and return the key/value pairs for each object. But I am having difficulty with the correct syntax. Here's what I'm trying to figure out:
let modelsToDisplay = [];
carBrands.forEach((object, i) => {
modelsToDisplay.push(<li key={i}>Make/Model/Units of each object</li>);
});
Thank you for your assistance :)