Trying to filter the results in array2
based on the value type
of array1
but the resultant array always prints the last value in the forEach loop.
Snippet
const array1 = [{
name: "Audi",
type: "sedan"
},
{
name: "BMW",
type: "sedan"
},
{
name: "Benz",
type: "suv"
}
];
const array2 = [{
year: "2020"
}, {
year: "2021"
}];
let a = [];
let customObject1 = {};
let array3 = [];
array1.forEach((val1) => {
array2.forEach((val2) => {
if (val1.type === "sedan") {
customObject1["label"] = "Sedan Car";
customObject1["year"] = val2.year;
array3.push(customObject1);
console.log(array3);
}
});
});
Here's the result that I'm expecting. Could anyone please help?
array3 = [{label: 'Sedan Car', year: '2020'}, {label: 'Sedan Car', year: '2021'}]