jonschlinkert / arr-diff

Returns an array with only the unique values from all given arrays using strict equality for comparisons.
https://github.com/jonschlinkert
MIT License
46 stars 18 forks source link

if It has object ,can not compare #7

Closed Yhaojing closed 7 years ago

Yhaojing commented 7 years ago
var diff = require('arr-diff');
var a= ["a", "b", "c", "d", [1, 2]];
var b= [ "b", "c", "d", "e", [1, 2], [3]];
console.log(diff(a, b)) 

expected => ['a', 'd', [3]] actual => ['a', [1, 2]]

The first, only find different a and b, not different b and a, The second, if It has object, can not compare 。Can you understand me?

edited by @doowb: code formating

doowb commented 7 years ago

@Yhaojing objects (and arrays) in JavaScript are compared by reference so comparing like this will give you false:

var a = [1, 2];
var b = [1, 2];
console.log(a === b);
//=> false

This is because they are actually 2 different arrays. If the arrays are the same instance, then you will get true:

var a = [1, 2];
var b = a;
console.log(a === b);
//=> true

I hope this helps. I'm going to close this issue, but if you have more questions, you can ask here or try Stack Overflow.