0

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

2 Answers 2

1

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)
}
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.