数组, Map 和 Set 的循环 : for of / forEach
对象的循环: for in
-
数组的 for 循环 – for (… of … ) / forEach / for (var i=0; i< length ; i++)
var a = [1,2,3,4,5]; for (var each of a){ console.log(each); }
var a = [1,2,3,4,5]; a.forEach(function (element){ console.log(element); })
var a = [1,2,3,4,5]; for (var i=0; i<a.length; i++){ console.log(a[i]); }
-
对象的 for 循环 – for (… in …) {…}
var obj1 = {'a':1, 'b':2, 'c':3}; for (var key in obj1){ console.log(key, obj1[key]); }
-
Map 和 Set 的 for 循环 – for ( … of …) / forEach()
var m = new Map([['a', 1], ['b',2], ['c', 3]]) for (var element of m){ console.log(element[0], element[1]); }
var m = new Map([['a', 1], ['b',2], ['c', 3]]) m.forEach(function (value, key, map){ console.log(key, value); })
var s = new Set([1,2,3,4,5]); s.forEach(function (element){ console.log(element); })