I have an object like this:
var obj = {
heroes: {
"1": {
label: "Spiderman"
},
"2": {
label: "Iron Man"
},
}
}
What I want to know it, whether there is an object e.g. 2 in obj.heroes.
I tried this, but it didn't work:
var name = "heroes"; //Will be a for-loop later
try {
if(name["2"] in obj)
console.log("There is an 2nd superhero!");
} catch(e) {console.log(e);}
..got only errors: "Cannot read property '2' of undefined"
I hope you are able to help me. Thanks
2in yournameobject (which is a string). Remove the property accessor from if(name["2"])