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?

1
  • Using jQuery you can do like var diff = $(a).not(b).get(); Commented Jan 23, 2017 at 8:18

1 Answer 1

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;
  })
)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.