1

I can't seem to select the first value inside the json object.
I've tried array[0][0], but it returns undefined.

[ { 'Room 1': 11017,
'Room 2': 2651,
'Room 3': 1,
'Room 4': 2,
'Room 5': 1,
'Room 6': 1,
'Room 7': 3,
'Room 8': 5,
'Room 9': 3661,
'Room 10': 2 } ]
Arunesh
  • 21
  • 5
  • 2
    Because there is no key as `0` in object..Try `array[0]['Room 1']` – Rayon Jun 29 '16 at 17:47
  • There *is no "first" value* with any guarantee. The solutions using `Object.keys()` are unsafe if the proper order is a requirement. –  Jun 29 '16 at 18:16

4 Answers4

4

You need to access keys using a string, not an index:

array[0]['Room 1']

Edit: There is a method to access keys based on order of definition. However, while almost all implementations of Object.keys currently return in the correct order, it is not required to do so by the spec:

var keys = Object.keys( array[0] );
var fifth = keys[4];
var value = array[0][fifth];
TbWill4321
  • 8,626
  • 3
  • 27
  • 25
  • Made an edit showing how that can work. – TbWill4321 Jun 29 '16 at 17:52
  • You guys really shouldn't post stuff that can fail. Beginners end up using solutions that have the *appearance* of working without heeding the warnings. –  Jun 29 '16 at 18:14
1

This is because it's an object inside the array. You can access an array's elements by using array[0], but for an object it'd have to be object.key or object["key"].

To make this data structure more dynamic, I'd suggest breaking it up like so:

roomNumbers = [11017, 2651, 1, 2, 1]...(and so on)

That way, you access a number without knowing the room ahead of time.

yazeedb
  • 133
  • 1
  • 10
0

You have an array, containing 1 object, which has keys and not indexes. So reference the first key:

array[0]['Room 1']

If you need to pull without know the key names, you can get them via Object.keys(obj);

var availableKeys = Object.keys(array[0]);
var val = array[0][availableKeys[0]]; //replace availableKeys index with what you need
tymeJV
  • 103,943
  • 14
  • 161
  • 157
0

Since the element is an object, it's trying to retrieve property 0 which is undefined. So you need to use the property name to get the value (eg : array[0]['Room 1']).


You can use Object.keys() to get the key array then get the value using that. But there is no guarantee of the order, since Object.keys() returns keys in an arbitrary order.

var array = [{
  'Room 1': 11017,
  'Room 2': 2651,
  'Room 3': 1,
  'Room 4': 2,
  'Room 5': 1,
  'Room 6': 1,
  'Room 7': 3,
  'Room 8': 5,
  'Room 9': 3661,
  'Room 10': 2
}]

console.log(
  array[0][Object.keys(array[0])[0]]
);

UPDATE : If you want to get it in certain order then use a nested array instead.

var array = [[
  11017,
  2651,
  1,
   2,
   1,
   1,
   3,
   5,
   3661,
   2
]]

console.log(
  array[0][0]
);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • Keep in mind also– If the items you are trying to access need to be in a specific order, objects do not maintain order. If the order is important, each of the key values should be in their own array index. – Nitsew Jun 29 '16 at 17:50
  • 1
    _`Object.keys` returns keys in arbitrary order_ – Rayon Jun 29 '16 at 17:51
  • @Arunesh : glad to help you :) – Pranav C Balan Jun 29 '16 at 18:04
  • @Arunesh: There's no guarantee that `[0]` will represent the `Room 1` key/value pair, so you shouldn't use this solution since it is allowed to produce any ordering it wants without warning. –  Jun 29 '16 at 18:11