This is a strange occurrence to be sure. First off, I’m using Chrome version 23.x and have not tried to reproduce this in other browsers yet.
I receive a JSON array from the server of the form:
JSON
{
"layout":0,
"caption":"Work History",
"cols":[
{"field":"company","label":"Company Name","hidden":false,"order":-1,"validationType":2,"list":[]}, {"field":"date_start","label":"From","hidden":false,"order":-1,"validationType":1,"list":[]},
{"field":"date_end","label":"Until","hidden":false,"order":-1,"validationType":0,"list":[]},
{"field":"position","label":"Title","hidden":false,"order":-1,"validationType":2,"list":[]},
{"field":"description","label":"Description","hidden":false,"order":-1,"validationType":0,"list":[]},
{"field":"project","label":"Project","hidden":false,"order":-1,"validationType":64,"list":[]}
]
}
Then, I attempt to loop over the cols objects with Javascript”
Javascript
for (var c in json.cols) {
console.log("col name: " + c);
}
Google Chrome’s console prints the following:
Output
col name: 0
col name: 1
col name: 2
col name: 3
col name: 4
col name: 5
col name: remove
There should only be 6 outputs, first of all. Second, where does this final “remove” key come from? Why is it listed?
Your
obj.json.colsis actually an array, not an object, so you should iterate over it with afor(;;)loop. I believe you’re looking for something like this:Furthermore, there seems to be some script messing with
Array.prototypeon your webpage, as with afor..inloop you should not be seeing aremoveproperty.