Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding missing element in two array for javascript [duplicate]

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?

like image 617
B.Rob Avatar asked Feb 06 '23 06:02

B.Rob


1 Answers

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;
  })
)
like image 131
Pranav C Balan Avatar answered Feb 08 '23 11:02

Pranav C Balan