Javascript 在两个数组中查找匹配值
我想找到两个数组之间的匹配值,如果值匹配,则创建一个json数组设置true;如果值不匹配,则创建一个json数组设置false。我知道,secondArray中的值将始终与第一个数组中的一些值匹配,并且它将始终更小,因为secondArray是基于第一个数组创建的Javascript 在两个数组中查找匹配值,javascript,arrays,typescript,match,Javascript,Arrays,Typescript,Match,我想找到两个数组之间的匹配值,如果值匹配,则创建一个json数组设置true;如果值不匹配,则创建一个json数组设置false。我知道,secondArray中的值将始终与第一个数组中的一些值匹配,并且它将始终更小,因为secondArray是基于第一个数组创建的 let firstArray = ["One", "Two", "Three", "Four", "Five"]; let secondArray = ["Three", "Four"]; let jsonArray = [];
let firstArray = ["One", "Two", "Three", "Four", "Five"];
let secondArray = ["Three", "Four"];
let jsonArray = [];
我想创建一个json数组:
[
{
"name": "One",
"matched": false
},
{
"name": "Two",
"matched": false
},
{
"name": "Three",
"matched": true
},
{
"name": "Four",
"matched": true
},
{
"name": "Five",
"matched": false
}
]
通常,我会这样做:
firstArray.forEach(firstElement=>{
secondArray.forEach(secondElement=>{
if(firstArray.indexOf(secondElement)>=0){
jsonArray.push({'name': secondElement, 'matched': true});
}else{
jsonArray.push({'name': secondElement, 'matched': false});
}
});
});
但这只是创建了一个json数组,其中名称相同,但匹配的值为false和true
似乎我在一件非常简单的事情中迷失了方向。使用th
find
检查元素是否存在
firstArray.forEach(secondElement=>{
let exist = secondArray.find((item) => item === secondElement);
if(exist){
jsonArray.push({'name': secondElement, 'matched': true})
}else{
jsonArray.push({'name': secondElement, 'matched': false})
}
});
试试这个:
let firstArray = ["One", "Two", "Three". "Four", "Five"];
let secondArray = ["Three", "Four"];
let jsonArray = [];
firstArray.forEach(firstElement=>{
if(secondArray.indexOf(firstElement)>=0){
jsonArray.push({'name': firstElement, 'matched': true});
}else{
jsonArray.push({'name': firstElement, 'matched': false});
}
});
希望此代码将帮助您我们可以使用
Array.prototype.includes()
检查数组中是否存在元素的步骤
让firstArray=[“一”、“二”、“三”、“四”、“五”];
设secondArray=[“三”,“四”];
让jsonArray=[];
firstArray.forEach(val=>{
if(secondArray.includes(val))
{
push({'name':val,'matched':true})
}否则{
推送({'name':val,'matched':false})
}
})
console.log(jsonArray)代码>这里的所有其他解决方案执行不必要的计算;它们的运行时间随着数组长度的平方而增长。尝试使用大小为100k+:-)的数组运行它们
您正在寻找的解决方案非常简单,可以在O(n)
中运行:
让firstArray=[“一”、“二”、“三”、“四”、“五”];
设secondArray=[“三”,“四”];
设map={};
forEach(i=>map[i]=false);
forEach(i=>map[i]==false&&(map[i]=true));
让jsonArray=Object.keys(map.map)(k=>({name:k,matched:map[k]}));
您可以使用map
和includes
助手的组合来实现这一点
让firstArray=[“一”、“二”、“三”、“四”、“五”];
设secondArray=[“三”,“四”];
让jsonArray=[];
jsonArray=firstArray.map(i=>{
返回{'name':i,'matched':secondArray.includes(i)};
});
console.log(jsonArray)代码>您可以通过两种方式完成此操作
Way1:
让数组1=['a','b','c','d']代码>
让array2=['b','d']代码>
让commonArray=[]代码>
array1.forEach( x => {
if(array2.indexOf(x) != -1){
commonArray.push(x);
}
});`
console.log(commonArray); //commonArray is the resulting array
Way2:使用下划线.js的交集
At first you have to import underscore:
import * as _ from 'underscore';`
Then use intersection to calculate common.
commonArray = _.intersection(array1, array2);
console.log(commonArray); //commonArray is the resulting array
你的代码中有很多拼写错误,你必须合并两个数组并将匹配设置为false,然后你必须将两个数组相交,并在合并后的代码中将值从相交设置为匹配truearray@PhilippSander我的代码哪里有拼写错误?请告诉我。不精确的应该是索引的。答案是正确的,但这一个代码较少。批准。@sie好的,那么你可以检查我的答案;)我看了,但现在我看到了地图和返回,它更漂亮了。谢谢分享我的想法