简体   繁体   中英

Javascript object is in array but can't access it

I have a for loop which iterates through currentObject in response.

This code:

console.log(response[currentObject]);

Clearly shows that response[currentObject] has among other properties, "start".

However, this code tells me the variable is undefined:

console.log(response[currentObject].start);

Why is this? Note that "start" is a date variable.

Here is the whole function:

function(response) {

                            for (var currentObject in response) {
                                //Parsing the data before its used
                                //"17\/10\/2012 20:55:00"
                                var phpStartDate = response[currentObject].start;
                                console.log(response[currentObject]);
                                var phpStopDate = response[currentObject].stop;
                                var datePartsStart = phpStartDate.match(/(\d+)/g);
                                var datePartsStop = phpStopDate.match(/(\d+)/g);
                                var parsedDateStart = new Date(datePartsStart[2], datePartsStart[1], datePartsStart[0], datePartsStart[3], datePartsStart[4], datePartsStart[5]);
                                var parsedDateStop = new Date(datePartsStop[2], datePartsStop[1], datePartsStop[0], datePartsStop[3], datePartsStop[4], datePartsStop[5]);

                                response[currentObject].start = parsedDateStart;
                                response[currentObject].stop = parsedDateStop;
                                //debugger;
                            };
                            return response;
                        }

Forgot to add, I'm using jQuery 1.8.2, which I believe handles dates differently than 1.7 (but I can't use 1.7 because I had a whole bunch of other problems with it!)

Here is the output of console.log(response):Object

data: Array[3]
0: Object
hourly: "4.00"
id: "40"
staff: "James Hadley"
start: "2012-09-25 00:00:00"
stop: "2012-09-27 00:00:00"
__proto__: Object
1: Object
hourly: "25.00"
id: "39"
staff: "James Hadley"
start: "2012-10-17 21:12:00"
stop: "2012-10-26 02:30:00"
__proto__: Object
2: Object
length: 3
__proto__: Array[0]
total: 3
__proto__: Object

I guess it's a timing issue, because the console.log object is live . Try

console.log(JSON.stringify(response[currentObject]));

I bet it does NOT include start even thought without stringify it does. Your response is asynchronous and you access start too early (your code doesn't show that part).

Now with your comment it's obvious. It's an array and you are missing and index, eg 0.

console.log(response[currentObject][0].start);

use

currentObject.start = parsedDateStart

instead of

response[currentObject].start = parsedDateStart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM