for in是ES5标准,遍历key.
for (var key in arr){
console.log(arr[key]);
}
for of是ES6标准,遍历value.
for (var value of arr){
console.log(value);
}
示例:
data数据:
"data":[{"id":"1","account":"admin","pwd":"123"},{"id":"2","account":"tony","pwd":"123"},{"id":"3","account":"root","pwd":"root"}]
for in:
var html = "";
for(var d in data){
html += "<tr><td>" + data[d].id + "</td><td>" + data[d].account + "</td><td>" + data[d].pwd + "</td></tr>";
}
for of:
var html = "";
for(var d of data){
html += "<tr><td>" + d['id'] + "</td><td>" + d['account'] + "</td><td>" + d['pwd'] + "</td></tr>";
}