尝试根据的array2值过滤结果,但结果数组始终打印 forEach 循环中的最后一个值。typearray1
片段
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);
}
});
});
这是我期待的结果。有人可以帮忙吗?
array3 = [{label: 'Sedan Car', year: '2020'}, {label: 'Sedan Car', year: '2021'}]
呼唤远方
MYYA
墨色风雨
慕工程0101907
相关分类