This php function returns a json encoded object to a javascript through ajax. I create a variable with this json object with stringify.

var event_id = JSON.stringify(rdata.event_id);

When I print that variable it looks like this.

[{"0":"e20111129215359"},{"0":"e20120301133826"},{"0":"e20120301184354"},{"0":"e20120301193226"},{"0":"e20120301193505"},{"0":"e20120303182807"},{"0":"e20120303205512"},{"0":"e20120303211019"},{"0":"e20120306182514"},{"0":"e20120307122044"}]

How do I access each element of event_id?

share|improve this question

79% accept rate
feedback

1 Answer

up vote 1 down vote accepted

Don't stringify it. It's already a valid JavaScript object, so just access it directly with:

rdata.event_id[0]["0"];
// e20111129215359

// Or read them in a loop
for (var i=0; i<rdata.event_id.length; i++) {
   console.log(rdata.event_id[i]["0"];
}

The value rdata.event_id is an array [] containing a bunch of object literals {} each having just one property "0". Since the property is a number instead of a string, you need to use the ["0"] syntax to access it, rather than the normal object dot operator.

share|improve this answer
This works. Thanks. – user823527 Mar 8 at 1:42
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.