skip to Main Content
var a=["a","b","c","d"];
var b=["b","a","c","d"];

I need output like:

mismatched array from a

mismatch array=["a", "b"];

2

Answers


  1. 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);
    Login or Signup to reply.
  2. 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"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search