lost9123193
lost9123193

Reputation: 11040

Using Difference from Lodash

I'm trying to set a new state for my react project and I'm stuck on what I'm doing wrong.I want to get the difference of 2 integer arrays

  const results = _.difference(items, currSelection);
  this.setState({ selected: results });

currSelection is:

 [1, 2, 3, 7]

item is:

 [1]

when I console.log results, I always get

[]

Upvotes: 0

Views: 2364

Answers (2)

Skylor.min
Skylor.min

Reputation: 11

_.difference(array, [values])

Creates an array of array values not included in the other given arrays >using SameValueZero for equality comparisons. The order of result values >is determined by the order they occur in the first array.

Arguments

array (Array): The array to inspect.

[values] (...Array): The values to exclude.

Returns

(Array): Returns the new array of filtered values.

Upvotes: 1

Kevin Le - Khnle
Kevin Le - Khnle

Reputation: 10887

Reverse the arguments as shown below:

const currSelection = [1, 2, 3, 7];
const items = [1];
const results = _.difference(currSelection, items);

console.log(results); //[2, 3, 7]

Upvotes: 3

Related Questions