我有一个字符串数组,如下所示:
let countries = ["PT", "ES", "FR"];以及一个对象数组,如下所示:
let people = [
{name: "Tom", country: "ES"},
{name: "Amy", country: "FR"},
{name: "Bee", country: "US"},
{name: "Ava", country: "PT"},
{name: "Kim", country: "JP"}
];我需要一个函数,接收国家的数组,,并返回一个数组,其中只有一个人有匹配的国家,在数组中的任何一个国家。
对于上面的数组,如果我将它们传递到函数中,我希望它返回一个只包含Tom、Amy和Ava的数组,因为只有它们来自位于countries数组中的国家。Bee和Kim不包括在内。
/:我试着在数组上使用.filter,但是我只能在硬编码国家的值时才能这样做,而我不能这样做,因为国家数组可以改变。如果我传递一个空数组,它应该调整并返回一个空数组。
const matchCriteria (countries) => {
// Can you help me please make such function?
}发布于 2021-06-17 12:36:36
很简单:
const countries = ["PT", "ES", "FR"],
people = [
{name: "Tom", country: "ES"},
{name: "Amy", country: "FR"},
{name: "Bee", country: "US"},
{name: "Ava", country: "PT"},
{name: "Kim", country: "JP"}
],
result = people.filter(({country}) => countries.includes(country))
console.log(result)
https://stackoverflow.com/questions/68019461
复制相似问题