How to compare two array and mismatch based on index using javascript? [duplicate]
var a=["a","b","c","d"];
var b=["b","a","c","d"];
I need output like:
mismatched array from a
mismatch array=["a", "b"];
You can use filter function and check value with the index in both the array and then you can get the mismatched values from a.
var a = ["a", "b", "c", "d"];
var b = ["b", "a", "c", "d"];
var c = a.filter(function (item, index) {
return item !== b[index];
});
console.log(c);
Here I used Javascript's filter method to get the mismatched Array.
const a = ["a","b","c","d"];
const b = ["b","a","c","d"];
const mismatchedArr = a.filter((aItem,index) => aItem !== b[index]);
console.log(mismatchedArr); //Prints ["a","b"]
精彩评论