英文:
Check if two arrays contain identical ids
问题
默认情况下,我有以大洲名称命名的数组。
europe= [];
northAmerica = [];
southAmerica = [];
asia = [];
而且我有两个包含一些数据的数组。
arr1 = [1,3];
arr2 = [
{ country: "巴西", id: 1, continent: "南美洲" },
{ country: "德国", id: 2, continent: "欧洲" },
{ country: "印度", id: 3, continent: "亚洲" },
{ country: "中国", id: 4, continent: "亚洲" },
];
如果在这两个数组中找到相匹配的id,我需要将字符串 obj.country 发送到相应大洲的数组中。
我尝试过这样做:
arr2.map((item) => {
if (arr1.find((id) => id === item.id)) {
if (item.continent === "南美洲") {
southAmerica.push(item.country);
}
if (item.continent === "亚洲") {
asia.push(item.country);
}
}
});
<details>
<summary>英文:</summary>
By default, I have arrays with the name of the continents.
europe= [];
northAmerica = [];
southAmerica = [];
asia = [];
And i have two arrays with some 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" },
];
If the id matches in the arrays, I need to send the string obj.country to the corresponding array of the continent.
I tried like this:
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);
}
}
});
But only the last value is sent to the arrays.
</details>
# 答案1
**得分**: 2
你想使用一个对象来更轻松地访问数组。你想要使用forEach和includes。
使用forEach:
```javascript
const continents = {
europe: [],
northAmerica: [],
southAmerica: [],
asia: [],
};
const arr1 = [1, 3];
const 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" },
];
arr2.forEach(data => {
if (arr1.includes(data.id)) {
continents[data.continent].push(data.country);
}
});
console.log(continents);
使用reduce:
const arr1 = [1, 3];
const 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" },
];
const continents = arr2.reduce((continents, data) => {
if (arr1.includes(data.id)) {
continents[data.continent].push(data.country);
}
return continents;
}, {
europe: [],
northAmerica: [],
southAmerica: [],
asia: []
});
console.log(continents);
英文:
You want to use an object to make accessing the arrays a lot easier. You want to use forEach and includes.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const continents = {
europe: [],
northAmerica: [],
southAmerica: [],
asia: [],
};
const arr1 = [1,3];
const 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" },
];
arr2.forEach(data => {
if (arr1.includes(data.id)) {
continents[data.continent].push(data.country);
}
});
console.log(continents);
<!-- end snippet -->
using reduce
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const arr1 = [1,3];
const 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" },
];
const continents = arr2.reduce((continents, data) => {
if (arr1.includes(data.id)) {
continents[data.continent].push(data.country);
}
return continents;
}, {
europe: [],
northAmerica: [],
southAmerica: [],
asia: []
});
console.log(continents);
<!-- end snippet -->
答案2
得分: 2
按照这样做。
const continents = {
europe: [],
northAmerica: [],
southAmerica: [],
asia: []
};
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" },
];
arr1.forEach(_id => {
arr2.find(obj => {
if (obj.id === _id) {
continents[obj.continent].push(obj.country);
return;
}
});
});
console.log(continents);
英文:
Do it like this.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const continents = {
europe: [],
northAmerica: [],
southAmerica: [],
asia: []
};
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" },
];
arr1.forEach(_id => {
arr2.find(obj => {
if (obj.id === _id) {
continents[obj.continent].push(obj.country);
return;
}
});
});
console.log(continents);
<!-- end snippet -->
答案3
得分: 0
const arr1Set = new Set(arr1);
let myArr = []
// Check if any object in arr2 has an ID that is also in arr1
const containsIdenticalIds = arr2.some(obj => {
if (arr1Set.has(obj.id)) {
myArr.push(obj.country)
}
});
console.log(myArr); // Output: true
英文:
const arr1Set = new Set(arr1);
let myArr = []
// Check if any object in arr2 has an ID that is also in arr1
const containsIdenticalIds = arr2.some(obj => {
if (arr1Set.has(obj.id)) {
myArr.push(obj.country)
}
});
console.log(myArr); // Output: true
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论