Ahmed
Ahmed

Reputation: 1359

How to find an exact match of a string in array of strings using underscore

I have the following string

const item = "CACHED ITEM 2"

and I have the following array

const names = ["CACHED ITEM 1"];

now I am using underscore to check if item in the array of name using the following code

if (_.contains(names),item){ 
    console.log(true)
}else{
    console.log(false)
}

If I run the above code the result is true event though item variable does not have an exact match in names array. How can I enforce exact match

Upvotes: 0

Views: 159

Answers (2)

In your code,

// item is passed as parameter to if , which ll be treated as OR     
if (_.contains(names),item)

_.contains

const item = "CACHED ITEM 2"
const names = ["CACHED ITEM 1"];
if (_.contains(names,item))
    console.log(true)
else
    console.log(false)

Upvotes: 1

Savan
Savan

Reputation: 102

You have to pass the 2nd parameter (item) inside contains's bracket.

See the below code.

if (_.contains(names, item)){ 
    console.log(true)
} else {
    console.log(false)
}

Upvotes: 1

Related Questions