遍历对象
//forin循环
let objs = [{
id: 1,
name: 'object1'
}, {
id: 2,
name: 'object2'
}, {
id: 3,
name: 'object3'
}]
for (var obj in objs) {
console.log('name ')
console.log(objs[obj]['name'])
}
//forin循环
let arrys = ['张三', '李四', '王五']
for (var arry in arrys) {
console.log('数组内容')
console.log(arrys[arry])
console.log('数组下标')
console.log(arry)
}
//for循环
let obj = {
id: 1,
name: 'object1'
}
let obj1 = {
id: 2,
name: 'object2'
}
let lists = []
lists.push(obj)
lists.push(obj1)
for (var i=0;i<=lists.length;i++){
console.log("集合对象属性");
console.log(lists[i]['name']);
}
遍历字典 (Map)
//forin循环
let dics = {
b: 2,c: 3, a: 1
}
for(var key in dics){
console.log("key: "+key+" value: "+dics[key])
}
console.log("字典元素按key值排序: ");
var res = Object.keys(dics).sort();
for(var key in res){
console.log("key: " + res[key] + " ,value: " + dics[res[key]]);
}
console.log("字典元素按value值排序: ");
var res2 = Object.keys(dics).sort(function(a,b){ return dics[a]-dics[b]; });
for(var key in res2){
console.log("key: " + res2[key] + " ,value: " + dics[res2[key]]);
}
|