comparing array.includes with database data by using If condition in javascript / node js



{
type:"fresh"
},
{
type:"marine",
},
{
type:"rank",
}

so my object somewhat look like this and i made a function which i want to check type by giving if condition so wrote a code like this

function test(req,res){
object.map((value)=>{
if(value.type=="fresh"|| value.type=="marine" || value.type=="tank"){
// do some condition
}

so I am putting all my condition in IF condition if and the problem is if there are more type condition then i have to do it like this value.type== “some”

  • so i was trying to put all this type in one array like this
let testType=["fresh","marine","tank"];

and trying to call it in my if condition as I know its work like this

if(testType.includes(true)){
// do some stuff
}

but how i can check with my database type do i have to do like this

if(value.type== testType.includes(true))

is this possible i can mention all my condition and then check with database ?

Answer

Array.prototype.includes() takes a value and checks if it is in the value. You are on the right track when you used testType.includes(), but you aren’t doing it correctly. You need to check if testType includes value.type:

if (testType.includes(value.type)) // Do stuff


Source: stackoverflow