JavaScript Object property always returns undefined [duplicate]

前提是你 提交于 2019-12-02 04:44:44

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

Try this

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

alert(registration[0].handle)

DEMO

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);
        });
    }

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

console.log(registration[0].handle); 

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!