lost9123193 lost9123193 - 2 months ago 13
Javascript Question

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

[]

Answer

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]