ie javascript for in loop vs chrome for in loop
This question already has an answer here:
using IE11 and looks ok to me
if you want more control use Array.prototype.forEach method.It accepts a callback which have three parameters.They are element,their index and the array itself.Try
var arr=["test",'ball'];arr.forEach(function(element,index){
console.log(index);
});
Use the hasOwnProperty
method.
Example:
var arr = ["test"];
for (var e in arr) {
if (arr.hasOwnProperty(e)) {
console.log(e);
}
}
Good explanation on why this is necessary: https://stackoverflow.com/a/12017703/1387396
Details on the hasOwnProperty
method and its use in for .. in
loops: Object.prototype.hasOwnProperty() - JavaScript | MDN
上一篇: 在枚举中检查定义的项目