检查两个数组是否包含相同的标识符。

huangapple go评论75阅读模式
英文:

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: &quot;Brazil&quot;, id: 1, continent: &quot;southAmerica&quot; },
  { country: &quot;Germany&quot;, id: 2, continent: &quot;europe&quot; },
  { country: &quot;India&quot;, id: 3, continent: &quot;asia&quot; },
  { country: &quot;China&quot;, id: 4, continent: &quot;asia&quot; },
];

arr2.forEach(data =&gt; {
  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: &quot;Brazil&quot;, id: 1, continent: &quot;southAmerica&quot; },
  { country: &quot;Germany&quot;, id: 2, continent: &quot;europe&quot; },
  { country: &quot;India&quot;, id: 3, continent: &quot;asia&quot; },
  { country: &quot;China&quot;, id: 4, continent: &quot;asia&quot; },
];

const continents = arr2.reduce((continents, data) =&gt; {
  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: &quot;Brazil&quot;, id: 1, continent: &quot;southAmerica&quot; },
  { country: &quot;Germany&quot;, id: 2, continent: &quot;europe&quot; },
  { country: &quot;India&quot;, id: 3, continent: &quot;asia&quot; },
  { country: &quot;China&quot;, id: 4, continent: &quot;asia&quot; },
];

arr1.forEach(_id =&gt; {
  arr2.find(obj =&gt; {
    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 =&gt; {
    if (arr1Set.has(obj.id)) {
        myArr.push(obj.country)
    }
});


console.log(myArr); // Output: true

huangapple
  • 本文由 发表于 2023年5月17日 20:24:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定