Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Lodash从集合中查找重复值_Javascript_Arrays_Json_Lodash - Fatal编程技术网

Javascript 使用Lodash从集合中查找重复值

Javascript 使用Lodash从集合中查找重复值,javascript,arrays,json,lodash,Javascript,Arrays,Json,Lodash,使用Lodash,我试图找出集合中是否存在值 如果存在,我想返回trueelsefalse const d = [{ "country": "India", "_id": ObjectId("5ad47b639048dd2367e95d48"), "cities": [] }, { "country": "Spain", "_id": ObjectId("5ad47b639048dd2367e95d49"), "cities": [

使用Lodash,我试图找出集合中是否存在值

如果存在,我想返回
true
else
false

 const d = [{
     "country": "India",
     "_id": ObjectId("5ad47b639048dd2367e95d48"),
     "cities": []
 }, {
     "country": "Spain",
     "_id": ObjectId("5ad47b639048dd2367e95d49"),
     "cities": []
 }];
代码片段

Countries = ['India', 'Spain']
if (_.has(d, Countries)) {
    console.log(true);
} else {
    console.log(false);
}

但它总是返回False。不建议我使用
lodash
如果可能,有人能建议一种更好的方法吗。

您可以使用
一些方法和
包含方法的组合。如果
items
中的任何项目包含
countries
数组中的国家,则它将返回
true

const项=[
{
“国家”:“印度”,
“_id”:“ObjectId”(“5ad47b639048dd2367e95d48”),
“城市”:[]
},
{
“国家”:“西班牙”,
“_id”:“ObjectId”(“5ad47b639048dd2367e95d49”),
“城市”:[]
}
];
const countries=[“印度”、“西班牙”];
const includes=\.some(items,item=>\.includes(countries,item.country));
控制台日志(包括)

您可以结合使用
某些方法和
包含方法。如果
items
中的任何项目包含
countries
数组中的国家,则它将返回
true

const项=[
{
“国家”:“印度”,
“_id”:“ObjectId”(“5ad47b639048dd2367e95d48”),
“城市”:[]
},
{
“国家”:“西班牙”,
“_id”:“ObjectId”(“5ad47b639048dd2367e95d49”),
“城市”:[]
}
];
const countries=[“印度”、“西班牙”];
const includes=\.some(items,item=>\.includes(countries,item.country));
控制台日志(包括)

您也可以使用内置的Javascripts
过滤器
功能在不使用Lodash的情况下执行此操作

d.filter(item => item.country === "Spain"); //Would return an array of objects where the country's name property is Spain!
如果我们想使其成为布尔值,可以将其声明为变量,并断言其长度大于0,如下所示:

let isSpanishCountry = d.filter(item => item.country === "Spain").length > 0; // console.log(isSpanishCountry) => true

您也可以使用内置的Javascripts
filter
函数在不使用Lodash的情况下执行此操作

d.filter(item => item.country === "Spain"); //Would return an array of objects where the country's name property is Spain!
如果我们想使其成为布尔值,可以将其声明为变量,并断言其长度大于0,如下所示:

let isSpanishCountry = d.filter(item => item.country === "Spain").length > 0; // console.log(isSpanishCountry) => true
ES6 您可以使用和来检查数组中的重复项

演示

const d=[{
“国家”:“印度”,
“_id”:“5ad47b639048dd2367e95d48”,
“城市”:[]
}, {
“国家”:“西班牙”,
“_id”:“5ad47b639048dd2367e95d49”,
“城市”:[]
}];
const Countries=[“印度”、“西班牙”];
console.log(d.some({country})=>Countries.includes(country))
.as控制台包装{最大高度:100%!重要;顶部:0;}
ES6 您可以使用和来检查数组中的重复项

演示

const d=[{
“国家”:“印度”,
“_id”:“5ad47b639048dd2367e95d48”,
“城市”:[]
}, {
“国家”:“西班牙”,
“_id”:“5ad47b639048dd2367e95d49”,
“城市”:[]
}];
const Countries=[“印度”、“西班牙”];
console.log(d.some({country})=>Countries.includes(country))

。作为控制台包装{max height:100%!important;top:0;}
但是在比较时有一个数组,没有一个值非常正确!我错过了那首歌。一些歌和歌曲是不错的选择!但是,虽然有一个数组,但没有一个值是非常正确的!我错过了那首歌。一些歌和歌曲是不错的选择!