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*

Michael
  • 2,016
  • 5
  • 35
  • 51
  • 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 – charlietfl Jun 07 '14 at 00:13
  • "`console.log(typeof(user_info)) = object`": You can check if something is an array using `user_info instanceof Array` http://stackoverflow.com/a/767492/482489 – JustcallmeDrago Jun 07 '14 at 00:15
  • 1
    `typeof` is an operator (not a function) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof , so it's more correct to use it without the function-call brackets, e.g. `typeof user_info` – jokeyrhyme Jun 07 '14 at 00:18

2 Answers2

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

prop - key what you need

Ivan Burlutskiy
  • 1,605
  • 1
  • 12
  • 22
  • 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.... – Michael Jun 07 '14 at 00:20
  • var first_key; for(var prop in user_info[0]){ first_key = prop; break; } console.log(first_key); – Ivan Burlutskiy Jun 07 '14 at 01:04
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.

bits
  • 8,110
  • 8
  • 46
  • 55
  • I just tried your suggestion in Chrome and I'm still getting the TypeError: blah blah blah... What version are you using of Chrome? – Michael Jun 07 '14 at 00:23
  • 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? – bits Jun 07 '14 at 00:25
  • Can you reproduce your issue in jsfiddle? – bits Jun 07 '14 at 00:28