0

I have a javascript object:

[{  id: 11,
username: 'me',
firstname: 'my',
lastname: 'name',
}]

I'm trying to get the value of the firstname property, but I can't seem to do it. I thought I knew how to work with objects until this. I'm developing this within Node.js, which I don't think matters, but I'm new to node, so who knows.

Console.log reads the following for my different attempts:

console.log(typeof(user_info)) = object

console.log(typeof(user_info[0])) = object

console.log(user_info) = [{ id: 11, username: 'me', firstname: 'my', lastname: 'name', }]

console.log(user_info[0].firstname) = TypeError: Cannot read property 'firstname' of undefined

console.log(user_info[0]['firstname']) = TypeError: Cannot read property 'firstname' of undefined

console.log(user_info['firstname']) = undefinded

console.log(user_info.firstname) = undefinded

var output = '';
for(var prop in user_info){
    output += prop + ': ' + user_info[prop] + '; ';
}
console.log(output);

=

0:[object Object];

for(var prop in user_info[0]){
    console.log(user_info[0][prop]);
}

=

11
me
my
name

I feel like I'm so close with the last shot, but I can't wrap my mind around how to get the key while using the for in loop. What am I missing?

**I'm using Firefox, if that helps. And though I like Chrome, in the current setting, I'm unable to use Chrome*

3
  • no way you can log user_info[0] as object and have it undefined when try user_info[0].firstname. Sounds like you have some asynchronous activity like ajax going on. provide a demo in jsfiddle.net that replicates problem Commented Jun 7, 2014 at 0:13
  • "console.log(typeof(user_info)) = object": You can check if something is an array using user_info instanceof Array stackoverflow.com/a/767492/482489 Commented Jun 7, 2014 at 0:15
  • 1
    typeof is an operator (not a function) developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… , so it's more correct to use it without the function-call brackets, e.g. typeof user_info Commented Jun 7, 2014 at 0:18

2 Answers 2

2
for(var prop in user_info[0]){
    console.log('key = ', prop);
    console.log('value = ', user_info[0][prop]);
}

prop - key what you need

Sign up to request clarification or add additional context in comments.

2 Comments

This is so strange to me. I can get the 'firstname' to show up if I use this loop, but I can't get anything other than undefined if I try to access it directly using console.log(user_info[0]['firstname'] outside of the loop. Within the loop it logs the firstname four times... Boggled....
var first_key; for(var prop in user_info[0]){ first_key = prop; break; } console.log(first_key);
1

You want key in for... in?

How about this...

for(var prop in user_info[0]){
    console.log(prop);
}

FWIW: console.log(user_info[0]['firstname']) prints 'my' in chrome.

3 Comments

I just tried your suggestion in Chrome and I'm still getting the TypeError: blah blah blah... What version are you using of Chrome?
Trust me, version of chrome has nothing to do with it. Are you sure nothing else is going wrong? Like AJAX calls messing with the variables of interest?
Can you reproduce your issue in jsfiddle?

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.