0

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

2
  • You're trying to access a property named 2 in your name object (which is a string). Remove the property accessor from if(name["2"]) Commented Jul 1, 2014 at 22:52
  • Duplicate of most of these: stackoverflow.com/… Commented Jul 1, 2014 at 23:31

4 Answers 4

1

Try

   if ("2" in obj[name]){
      console.log("There is an 2nd superhero!");
   }

But if you are trying to identify counts, it might be better if you used arrays

var obj = {
   heroes: [
            {label: "Spiderman"},
            {label: "Iron Man"}
           ]
}

And check with

if (obj[name].length > 1) {
    console.log("There is an 2nd superhero!");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Where exactly is the difference? Perfomance? Good style? Just for upgrading my basic knowledge.
if the "2" has intrinsic value then you should keep your original code, if - on the other hand - the 2 is just to signify that it is the second item, then you should use an array. It is the right tool for the job, plus it has properties and methods better suited to iterating/counting/adding/removing.. etc..
1

You can do something like:

try {
  console.log(obj.heroes["2"]);
} catch (e) {
  console.log('nope :c');
}

However, it would be better to store heroes as an array:

var obj = {
  heroes: [
    {
      label: 'Spiderman'
    },
    {
      label: 'Ironman'
    }
  ]
};

Using the array makes a little more sense since heroes consists of multiple hero objects.

Comments

0

If 2nd superhero doesn't exist the condition return false.

if(obj.heroes["2"]) 
    console.log("There is an 2nd superhero!");

Or :

var count = 0;
for (var x in obj.heroes) {
    if (obj.heroes.hasOwnProperty(x)) {
       count++;
    }
}
console.log("You see "+ count +" heroes.");

Comments

0

this code will hunt it down for you

var delve = function(object, property, dodge) {
        if (!dodge) dodge = object;
        for (var i in object) {
            if (object[i] === dodge) continue;
            if (typeof(object[i]) == typeof({})) this.delve(object[i], property, dodge)
            if (i == property) console.log(object[i]);
        }
    }
delve(heroes,'2')

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.