Lodash: _.difference and _.merge on two different arrays with common elements

Created on 6 Jun 2014  Â·  3Comments  Â·  Source: lodash/lodash

I don't know how those functions should work, but this:

_.merge(['asd', 'asd', 1], ['asd', 'asd', 2])

don't return ['asd', 'asd', 1, 2] but ["asd", "asd", 2]. And:

_.difference(['asd', 'asd', 1], ['asd', 'asd', 2])

return [1] instead of [1,2].

Is that how those function should work? I'm using version 2.2.1

question

Most helpful comment

Difference is correct per documentation

_.difference(array, [values])
Creates an array excluding all values of the provided arrays using strict equality for comparisons, i.e. ===.

_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
// → [1, 3, 4]

_.merge is an object function thus we can expect it to merge by keys

All 3 comments

Difference is correct per documentation

_.difference(array, [values])
Creates an array excluding all values of the provided arrays using strict equality for comparisons, i.e. ===.

_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
// → [1, 3, 4]

_.merge is an object function thus we can expect it to merge by keys

As for the difference, this is the best I can think of

_.chain(array1.concat(array2, ...))
 .countBy()
 .pick(function(v) {return v === 1})
 .keys()
 .value()

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ryyppy picture ryyppy  Â·  3Comments

SameerSiddiqui picture SameerSiddiqui  Â·  3Comments

gajus picture gajus  Â·  3Comments

sarahquigley picture sarahquigley  Â·  3Comments

dengyaolong picture dengyaolong  Â·  3Comments