jquery - Compare 2 arrays and remove duplicates -


i trying compare 2 arrays using jquery , remove duplicates that.
this code.is logic correct?

var list1 = [6, 7, 3, 4, 1, 2]; var list2 = [2, 4, 6, 5, 1, 9, 8, 7, 8];  var newarray = []; var index1, index2; $.each(list1, function(i, value)) {   index1 = $.inarray(list1[i]);   index2 = $.inarray(newarray[i]);   if (index2 == -1) {     newarray.push(list2[i]);   } } 

expected output:

[3,5,9,8] 

try this

<script type="text/javascript">     var arr1=[6,7,3,4,1,2];     var arr2=[2,4,6,5,1,9,8,7,8];     $(document).ready(function(){         var newarray=$.merge($(arr1).not(arr2).get(),$(arr2).not(arr1).get());         console.log(newarray);     }); </script> 

it give out put as

[ 3, 5, 9, 8,8 ] 

another answer with using $.each,$.inarray , .push try

var list1 = [6, 7, 3, 4, 1, 2]; var list2 = [2, 4, 6, 5, 1, 9, 8, 7, 8];  var newarray = []; $.each(list1, function(i, value){     if($.inarray(value,list2)==-1){         newarray.push(value);     } }); $.each(list2, function(i, value){     if($.inarray(value,list1)==-1){         newarray.push(value);     } }); newarray=$.unique(newarray); console.log(newarray); 

it give out put as

[ 3, 5, 9, 8] 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -