Nick
Nick

Reputation: 14283

Lodash _.difference returns an empty array

Quick and simple question:

I have two arrays that look like this:

var arr1 = [10037, 8812, 2412]
var arr2 = [10037, 8813, 2405, 8815, 2407, 8812, 2412, 2412, 8815]

I use lodash difference() to generate a new variable:

var difference = _.difference(arr1, arr2)

I console log difference and i am expecting to see something like this:

[8813, 2405, 8815, 2407,2412,8815] but instead i get an empty array.

Based on the documentation, difference should give back a new array containing the difference beween the two, so why am i getting a new empty array here? what am i doing wrong?

Thanks for any explanation

Upvotes: 1

Views: 1322

Answers (1)

Bo Borgerson
Bo Borgerson

Reputation: 1406

_.difference returns values from the first array that are not present in any of the other arrays passed in.

All values in arr1 are also present in arr2, so the result is an empty array.

Documentation for _.difference is available here: https://lodash.com/docs/4.17.4#difference

Upvotes: 6

Related Questions