简体   繁体   English

将一个数组的每个元素与另一个数组进行比较,然后找到未找到的元素

[英]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.

相关问题 在数组中,将每个元素与每个元素进行比较并显示差异 - In an array, compare each element with each one and display the differences 如何从在另一个数组中找到的一个数组中递增每个字符串元素? - How to increment each string element from one array that is found in another array? 将一个值与另一个数组中的每个数组值进行比较 - compare one value with each value of array which is inside another array 比较另一个数组中两个数组的元素 - compare element's of two arrays which are in another array 在Javascript中查找另一个数组中数组的每个元素的所有出现 - Find all occurrences of each element of an array in another array in Javascript 如何按升序替换另一个数组中未找到的元素的值? - How to replace the value of element which is not found in another array in ascending order? 在一个数组中查找长度不等的另一个数组中的元素JavaScript - Find element in one array that's in another array unequal lengths JavaScript 将一个数组的每个元素乘以另一个数组的部分的方法? - Way to multiply each element of one array to PARTS of another? 将数组中的每个元素与其他所有元素进行比较 - Compare each element in an array against every other element 4.用一个参数(位置)定义一个function,通过存储在数组中的position找到一个元素并返回找到的元素的详细信息 - 4. Define a function with one parameter (position) to find an element by the position stored in the array and to return the details of element found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM