Home > database >  How would I check if string is equal to a value in an object [closed]
How would I check if string is equal to a value in an object [closed]

Time:10-04

I have an object called codes:

var codes = {
    code1: 'test1'
    code2: 'test2'
  }

And i want to check if it has a property and log the result to the console

if(input == what goes here)
{
   console.log("Has property")
}


Sorry if its a very obvious answer, I am pretty new to Javascript

CodePudding user response:

You can use hasOwnProperty():

var codes = {
    code1: 'test1',
    code2: 'test2'
  }


console.log(codes.hasOwnProperty('code1'));
console.log(codes.hasOwnProperty('code2'));
console.log(codes.hasOwnProperty('code3'));

if(codes.hasOwnProperty(input)){

}
  • Related