[英]Compare each element of one array to another and find which element is not found
I have two array which contains special characters am trying to compare each element of one array to another and get the element which is not found in another array. 我有两个包含特殊字符的数组,试图将一个数组的每个元素与另一个数组进行比较,并获得在另一个数组中找不到的元素。 But my approach doesnt work properly 但是我的方法不能正常工作
var specialChar = ['!','@','#','$','%','&']; var specialChar = ['!','@','#','$','%','&']; var $scope.inp= ['!','*','#']; var $ scope.inp = ['!','*','#'];
In my above example element '*' is not present specialChar 在我上面的示例中,元素'*'不存在specialChar
I tried this logic - 我尝试了这种逻辑-
$scope.validateChar = function(specialChar,inp){
var i,j;
for (i=0,j=0; i<specialChar.length && j<inp.length;) {
if (specialChar[i] < inp[j]) {
++i;
} else if (specialChar[i] == inp[j]) {
++i; ++j;
} else {
$scope.notFoundChar = inp[j];
Error prompt showing special charatcter $scope.notFoundChar not found 错误提示显示特殊字符$ scope.notFoundChar未找到
$scope.charAllowedText = false;
return;
}
}
}
Please suggest what am doing wrong here? 请在这里提出问题所在?
You can filter out your Special char '*' like below 您可以像下面那样过滤掉特殊字符“ *”
var result=[]
inp.map(function(inpElement){
if(specialChar.indexOf(inpElement)==-1)
result.push(inpElement)
})
console.log(result)
Below given code solves your problem. 下面给出的代码解决了您的问题。
var source = [1,2,3,4,5,6,7,8]; var target =[2,3,4,5,6,18,19]; var missingItems = []; target.forEach(function(itemFromTarget){ var itemFound = false; source.forEach(function(itemFromSrc){ if(itemFromTarget === itemFromSrc){ itemFound = true; } }); if (!itemFound) { missingItems.push(itemFromTarget); } }); console.log(missingItems);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.