javascript - compare 2 arrays and get those variables did not match -


var array1 = ["display1"]; var array2 = ["display1", "display2", "display3"];    array.prototype.compare = function(testarr) {       if (this.length != testarr.length) return false;       (var = 0; < testarr.length; i++) {           if (this[i].compare) {               if (!this[i].compare(testarr[i])) return false;           }           if (this[i] !== testarr[i]) return false;       }       return true;   }    if(!array1.compare(array2)) {     alert("is not match");     // un-matched variable.   }else{     alert("matched");   } 

my intetion compare 2 array each other , , un-matched variable out , code compare see if matched or not.

how un-matched variable ??

so result ["display2", "display3"];

you use temporary object count given values of array1 , array2 , filter if value has count 1.

function xxx(a1, a2) {      var a3 = a1.concat(a2),          temp = object.create(null);        a3.foreach(function (a) {          temp[a] = (temp[a] || 0) + 1;      });      return a3.filter(function (a) {          return temp[a] === 1;      });  }    var array1 = ["display1"],      array2 = ["display1", "display2", "display3"],      array3 = xxx(array1, array2);    document.write('<pre>' + json.stringify(array3, 0, 4) + '</pre>');

es6

function xxx(a1, a2) {      var a3 = a1.concat(a2),          temp = object.create(null);        a3.foreach(a => temp[a] = (temp[a] || 0) + 1);      return a3.filter(a => temp[a] === 1);  }    var array1 = ["display1"],      array2 = ["display1", "display2", "display3"],      array3 = xxx(array1, array2);    document.write('<pre>' + json.stringify(array3, 0, 4) + '</pre>');


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -