Below is an a function that contains of an object, and I want to be able to access a specific body within the object with the use of an argument, that is with an index.
The problem is that when I try to access a property this way I get
undefined
when console logs it. What am I doing wrong?
moveLeftBtn.on('click', function(nr){
var theBody = {
bodies: {
a: 'foo1',
b: 'foo2',
c: 'foo3'
}
};
var newBody = theBody.bodies[1]; // temporarily hardcoded value instead of argument
console.log(newBody); // <-- undefined, why?
return newBody;
});
EDIT
If I console log theBody.bodies
I can see it's value (Object {a: Array[5], b: Array[5], c: Array[5]}
), but when I try to access it's properties with [1]
I get
undefined
(even though the properties contains of strings).