How do I access this Javascript array (JSON object?)?

Go To StackoverFlow.com

1

My problem is that the database fields have been badly designed by previous people before me. We are accessing the table rows horizontally.

I am working with PHP and jQuery right now and I am passing data back and forth through AJAX. In PHP I am accessing this array like so:

$attendance['date_lesson_' . $lessonCount] 

where lessonCount is a number from 1-10 that is incremented i.e. if lessonCount = 1 then above would be $attendance['date_lesson_1']

I am passing the attendance array through json_encode

How do I therefore access this field data_lesson_ 1 up to 10 in jQuery?

I am trying to do:

var lessonCount = 1;

attendance[i].date_lesson_+lessonCount

//do some stuff with attendance.date_lesson_ 
lessonCount++;

It was easy in PHP cause you concatenate strings with the dot "." but this isn't a string I'm dealing with in jQuery/JavaScript so how on earth would I do this?

Yes, I know. This is awkward. I agree 100%.

2012-04-03 20:38
by Johnathan Au


4

You’ll need to use bracket notation:

attendance['date_lesson_' + lessonCount]

Dot notation can only be used with identifier names as property names.

2012-04-03 20:40
by Mathias Bynens
Cheers mate :) - Johnathan Au 2012-04-03 20:47


0

Just a guess, I don't know how that json looks like.

attendance["date_lesson_"+lessonCount]
2012-04-03 20:43
by mihai
I refrained from showing the JSON because it's 50 fields long... (when it could have been 5 - Johnathan Au 2012-04-03 20:45
but either way, it works : - Johnathan Au 2012-04-03 20:46
Ads