1

Tell me what I am missing here. I have the follow javascript object.

[ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD',
    handle: '123',
    userId: 'ABC123'} ]

When I do the following

success: function (registration) {
                console.log(registration);
                console.log(registration.handle); 

Console log writes out the object as defined above. However when I do registration.handle I get an error saying "undefined." If registration is the above object why does registration.handle not work?

what am I missing?

Just code
  • 13,553
  • 10
  • 51
  • 93
ToddB
  • 1,464
  • 1
  • 12
  • 27

5 Answers5

4

You have an array containing an object. The properties you are trying to access are members of the object, not the array.

You must first get a reference to the object before you access its properties.

registration[0].handle
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Try this

var registration=[ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'} ]

alert(registration[0].handle)

DEMO

Sridhar R
  • 20,190
  • 6
  • 38
  • 35
1

You are accessing the member of an object.

Do it like this way

success: function(registration) {
        $.each(registration, function(index, data) {
            var handle = data.handle;
            console.log('id is getting now ' + handle);
        });
    }
1

Yes you first need to access array element then you can find object

console.log(registration[0].handle); 
deepak
  • 346
  • 1
  • 5
0

it is because you are having array so to access it try

  registration[0].handle

EXAMPLE

CASE 1

registration =  [ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'} ];
console.log(registration[0].handle);

CASE 2

registration = { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'};
console.log(registration.handle);
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40