0

I have an object named as "obj" and it has two keys named as "goal" and "item[]", ie.

var obj = {goal:"abc",item[]:"def"};

And these keys and values are created dynamically.

Problem -

I want to check if these keys exist or not. If I check

if(obj.goal != undefined){
   //Do something
}

then it gives desired output. but when I do

if(obj.item[] != undefined){
  //Do something
}

then it throw error(Syntax error). Please advice how to check with the second case. Thanks in advance.

sajalsuraj
  • 922
  • 15
  • 29

1 Answers1

1

Please use this with bracket notation:

if (obj['item[]'] !== undefined) {
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392