Uland Nimblehoof Uland Nimblehoof - 1 month ago 6
Javascript Question

Lodash - method which return non-share elements from two arrays

Helo guys, could you help with:

I need a method to lodash which return:

_.method( [1,2,3,4,5,6,7], [1,2,3] ) => [4,5,6,7]


In other words method which return non-share elements.

Best regards
Greg.

Answer

You could use _.difference.

Creates an array of array values not included in the other given arrays using SameValueZero for equality comparisons. The order and references of result values are determined by the first array.

var result = _.difference([1, 2, 3, 4, 5, 6, 7], [1, 2, 3]);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>