5

I have a problem in Node.js.. My problem is two arrays comparing. For example;

My original array is;

var a = ["1","2","3","4","5"];

and the other array is;

var b = ["3","1","4","6","8","7"];

so, result message what I want is: "2 and 5 is missing the original array.."

So how can I get this message after comparison of two arrays?

B.Rob
  • 77
  • 1
  • 2

1 Answers1

12

Use Array#filter method to filter array elements.

var a = ["1", "2", "3", "4", "5"];
var b = ["3", "1", "4", "6", "8", "7"];

console.log(
  a.filter(function(v) {
    return !b.includes(v);
  })
)

// or for older browser

console.log(
  a.filter(function(v) {
    return b.indexOf(v) == -1;
  })
)
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188