Rectangle 27 0

When iterating JSON with Javascript for...in, the final key returned is "remove"?


for (var i=0; i<json.cols.length; i++) {
    console.log("col name: " + json.cols[i].label);
}

Furthermore, there seems to be some script messing with Array.prototype on your webpage, as with a for..in loop you should not be seeing a remove property.

I have an Array.prototype.remove() specified, yes. I guess that explains where it is coming from. Thanks for reminding me.

I tested here, the result is the same.

So your object is not what you think, see jsfiddle.net/T2hUX

Your obj.json.cols is actually an array, not an object, so you should iterate over it with a for(;;) loop. I believe you're looking for something like this:

Note
Rectangle 27 0

When iterating JSON with Javascript for...in, the final key returned is "remove"?


for (var c in json.cols) {
    if( json.cols.hasOwnProperty(c) ) {
      console.log("col name: " + c);
    }
}
if( json.cols.hasOwnProperty(c) ) { ... }

A for...in loop iterates over the properties of an object in an arbitrary order

I don't see the problem of his implementation with for...in, he used correctly the hasOwnProperty how ever, the for loop is a faster iterator. I don't think give @Chase a down vote is honest.

If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames or perform a hasOwnProperty check (propertyIsEnumerable can also be used).

If you wouldn't mind, please let me know why I was down voted. If you feel the information is incorrect then I will gladly fix it =).

Probably because there is no reason to use for...in on arrays, a normal for loop will do just fine (it's faster too).

Yeah, I completely agree on the relevance of the for...in vs a for, however, I mainly wanted to specify what the issue actually was, instead of just saying "Change it to a for loop". Thanks though @FelixKling, you're probably correct. Since @bfavaretto already has the for loop difference, there's little reason for me to add it to my answer, however, it could be beneficial to some, so I'll leave it here for now.

Note
Rectangle 27 0

When iterating JSON with Javascript for...in, the final key returned is "remove"?


for (var c in json.cols) {
    if( json.cols.hasOwnProperty(c) ) {
      console.log("col name: " + c);
    }
}
if( json.cols.hasOwnProperty(c) ) { ... }

A for...in loop iterates over the properties of an object in an arbitrary order

I don't see the problem of his implementation with for...in, he used correctly the hasOwnProperty how ever, the for loop is a faster iterator. I don't think give @Chase a down vote is honest.

If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames or perform a hasOwnProperty check (propertyIsEnumerable can also be used).

If you wouldn't mind, please let me know why I was down voted. If you feel the information is incorrect then I will gladly fix it =).

Probably because there is no reason to use for...in on arrays, a normal for loop will do just fine (it's faster too).

Yeah, I completely agree on the relevance of the for...in vs a for, however, I mainly wanted to specify what the issue actually was, instead of just saying "Change it to a for loop". Thanks though @FelixKling, you're probably correct. Since @bfavaretto already has the for loop difference, there's little reason for me to add it to my answer, however, it could be beneficial to some, so I'll leave it here for now.

Note
Rectangle 27 0

When iterating JSON with Javascript for...in, the final key returned is "remove"?


for (var i=0; i<json.cols.length; i++) {
    console.log("col name: " + json.cols[i].label);
}

Furthermore, there seems to be some script messing with Array.prototype on your webpage, as with a for..in loop you should not be seeing a remove property.

I have an Array.prototype.remove() specified, yes. I guess that explains where it is coming from. Thanks for reminding me.

I tested here, the result is the same.

So your object is not what you think, see jsfiddle.net/T2hUX

Your obj.json.cols is actually an array, not an object, so you should iterate over it with a for(;;) loop. I believe you're looking for something like this:

Note
Rectangle 27 0

When iterating JSON with Javascript for...in, the final key returned is "remove"?


for (var i=0; i<json.cols.length; i++) {
    console.log("col name: " + json.cols[i].label);
}

Furthermore, there seems to be some script messing with Array.prototype on your webpage, as with a for..in loop you should not be seeing a remove property.

I have an Array.prototype.remove() specified, yes. I guess that explains where it is coming from. Thanks for reminding me.

I tested here, the result is the same.

So your object is not what you think, see jsfiddle.net/T2hUX

Your obj.json.cols is actually an array, not an object, so you should iterate over it with a for(;;) loop. I believe you're looking for something like this:

Note
Rectangle 27 0

When iterating JSON with Javascript for...in, the final key returned is "remove"?


for (var i=0; i<json.cols.length; i++) {
    console.log("col name: " + json.cols[i].label);
}

Furthermore, there seems to be some script messing with Array.prototype on your webpage, as with a for..in loop you should not be seeing a remove property.

I have an Array.prototype.remove() specified, yes. I guess that explains where it is coming from. Thanks for reminding me.

I tested here, the result is the same.

So your object is not what you think, see jsfiddle.net/T2hUX

Your obj.json.cols is actually an array, not an object, so you should iterate over it with a for(;;) loop. I believe you're looking for something like this:

Note
Rectangle 27 0

When iterating JSON with Javascript for...in, the final key returned is "remove"?


for (var c in json.cols) {
    if( json.cols.hasOwnProperty(c) ) {
      console.log("col name: " + c);
    }
}
if( json.cols.hasOwnProperty(c) ) { ... }

A for...in loop iterates over the properties of an object in an arbitrary order

I don't see the problem of his implementation with for...in, he used correctly the hasOwnProperty how ever, the for loop is a faster iterator. I don't think give @Chase a down vote is honest.

If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames or perform a hasOwnProperty check (propertyIsEnumerable can also be used).

If you wouldn't mind, please let me know why I was down voted. If you feel the information is incorrect then I will gladly fix it =).

Probably because there is no reason to use for...in on arrays, a normal for loop will do just fine (it's faster too).

Yeah, I completely agree on the relevance of the for...in vs a for, however, I mainly wanted to specify what the issue actually was, instead of just saying "Change it to a for loop". Thanks though @FelixKling, you're probably correct. Since @bfavaretto already has the for loop difference, there's little reason for me to add it to my answer, however, it could be beneficial to some, so I'll leave it here for now.

Note
Rectangle 27 0

When iterating JSON with Javascript for...in, the final key returned is "remove"?


for (var c in json.cols) {
    if( json.cols.hasOwnProperty(c) ) {
      console.log("col name: " + c);
    }
}
if( json.cols.hasOwnProperty(c) ) { ... }

A for...in loop iterates over the properties of an object in an arbitrary order

I don't see the problem of his implementation with for...in, he used correctly the hasOwnProperty how ever, the for loop is a faster iterator. I don't think give @Chase a down vote is honest.

If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames or perform a hasOwnProperty check (propertyIsEnumerable can also be used).

If you wouldn't mind, please let me know why I was down voted. If you feel the information is incorrect then I will gladly fix it =).

Probably because there is no reason to use for...in on arrays, a normal for loop will do just fine (it's faster too).

Yeah, I completely agree on the relevance of the for...in vs a for, however, I mainly wanted to specify what the issue actually was, instead of just saying "Change it to a for loop". Thanks though @FelixKling, you're probably correct. Since @bfavaretto already has the for loop difference, there's little reason for me to add it to my answer, however, it could be beneficial to some, so I'll leave it here for now.

Note