Initially, my arrays are named after the different continents of the world.
europe= [];
northAmerica = [];
southAmerica = [];
asia = [];
In addition to these, I also have two arrays containing some specific data.
arr1 = [1,3];
arr2 = [
{ country: "Brazil", id: 1, continent: "southAmerica" },
{ country: "Germany", id: 2, continent: "europe" },
{ country: "India", id: 3, continent: "asia" },
{ country: "China", id: 4, continent: "asia" },
];
My objective is to match IDs from both arrays and then push the corresponding country name into the respective continent array.
This was my initial attempt:
arr2.map((item) => {
if (arr1.find((id) => id === item.id)) {
if (item.continent === "southAmerica") {
southAmerica.push(item.country);
}
if (item.continent === "asia") {
asia.push(item.country);
}
}
});
Unfortunately, only the last value matches and gets pushed into the arrays.