Compare 2 array's elements against each other and return count JavaScript

后端 未结 3 681
花落未央
花落未央 2021-01-14 17:45

I have 2 arrays that I need to compare against each other and return the count of the same.

Example: compare array1 [abcd] against array2 [adce]. Return would be 2,1

3条回答
  •  旧巷少年郎
    2021-01-14 18:10

    You get 1 as output because length is not defined in your code

    var array1 = ['a','b','c','d'];
    var array2 = ['a','d','c','e'];
    
    var length = Math.min(array1.length,array2.length);
    var countMatched = 0,countNotMatched = 0;
    
    for(var index=0;index= 0)
        countNotMatched++;
    }
    alert(countMatched );
    alert(countNotMatched);
    

    Demo Fiddle : http://jsfiddle.net/tCKE7/2/

提交回复
热议问题