Reputation: 1359
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
Reputation: 81
In your code,
// item is passed as parameter to if , which ll be treated as OR
if (_.contains(names),item)
const item = "CACHED ITEM 2"
const names = ["CACHED ITEM 1"];
if (_.contains(names,item))
console.log(true)
else
console.log(false)
Upvotes: 1
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