-4

I have an javascript array of objects as follows :

list = [
            {
                "employee_work_rights_id":74,
                "amazon_id":173,
                "employee_id":3,
                "work_rights":"australian_citizen",
                "document_type":"password",
                "display_name":"a.pdf",
                "filename":"abc.pdf",
                "s3bucket":"xyz",
                "filepath":"employer27\/employee3\/"

            },

            {
                "employee_work_rights_id":75,
                "amazon_id":175,
                "employee_id":3,
                "work_rights":"australian_citizen",
                "document_type":"password",
                "display_name":"a.pdf",
                "filename":"xyz.pdf",
                "s3bucket":"zyx",
                "filepath":"employer27\/employee3\/"
            }
        ]

I tried to access amazon_id as follows :

console.log(list[0].amazon_id);

This is giving me undefined. How can I access this ?

Rumel
  • 319
  • 1
  • 3
  • 19

1 Answers1

3

You're doing the right thing. The way you initialize list is correct, and so is the way you access the property.

Assuming you were trying interactively in the console, the undefined you see is not the value of list[0].amazon_id. It's the return value of console.log. In Javascript, everything has a return value. However, in the console, just above or just below your undefined, you should see amazon_id's proper value.

Mathias Dolidon
  • 3,775
  • 1
  • 20
  • 28
  • 1
    A big stab in the dark this one is... :) – deceze Apr 11 '16 at 08:57
  • There's some extrapolation indeed, but it's the only reasonable one. :) – Mathias Dolidon Apr 11 '16 at 08:59
  • Well, there's also the good old http://stackoverflow.com/q/14220321/476... :) – deceze Apr 11 '16 at 09:00
  • 1
    Not here, no. Accessing an unexisting property inside an unexisting list would throw an exception. So we're not likely in the case where variable scope has been badly handled or understood. – Mathias Dolidon Apr 11 '16 at 09:02
  • Thanks a lot @MathiasDolidon. After reading your comment, I checked console again and debugged it with debugger. What happened is there was a double quote " before the array. Console does not print that. Here I have just copy pasted the array so here it is working correctly. I have to $.parseJSON() this for the correct answer. However, your answer triggered the solution. Upvoted you. :) – Rumel Apr 11 '16 at 09:05
  • 1
    Well then honestly the stab in the dark missed its target nonetheless and Deceze was right. :D – Mathias Dolidon Apr 11 '16 at 09:07
  • 1
    @Tanvir : when debugging JS, each browser has its pros/cons. When it comes to console.log, you'll notice that Firefox is very explicit about telling you the exact type of what's printed. When you console.log an array, you'll get `Array [...]`. Not so with Chrome, which only gives you a subtle hint with italic characters. Might help to know it. – Mathias Dolidon Apr 11 '16 at 09:14