BrianM
BrianM

Reputation: 115

Using _.some in lodash

I have Typescript code where i want to return true if any items in 1 array are present in another array. I am new to lodash but i was looking to do this using _.some. I am not sure if this is the correct approach. The code below is returning false but i expected it to return true.

let array1 = ["test", "some", "lodash"];
let array2 = ["some", "includes"];

let condition : boolean = _.some(array1, array2);

Upvotes: 0

Views: 8889

Answers (2)

baao
baao

Reputation: 73241

lodash was cool before plain javascript had the same methods...

let array1 = ["test", "some", "lodash"];
let array2 = ["some", "includes"];

let test = array1.some(e => array2.includes(e));

console.log(test);

Upvotes: 0

hsz
hsz

Reputation: 152226

You can use intersection function and check if it returns any items:

let condition : boolean = _.intersection(array1, array2).length > 0;

With some you have to pass a test callback as a second argument:

let condition : boolean = _.some(array1, item => array2.includes(item))

Upvotes: 4

Related Questions