iterable(遍历)

(一)遍历Array可以采用下标循环,遍历MapSet就无法使用下标。为了统一集合类型,ES6标准引入了新的iterable类型,ArrayMapSet都属于iterable类型。

具有iterable类型的集合可以通过新的for ... of循环来遍历。

    遍历数组:

var arr=['1',2,4,121];
for(var x of arr)
{console.log(x);}

   遍历Map对象:

 var m=new Map([[1,'x'],[2,'y'],[3,'z']]);

 for(var t of m)

    {console.log(t[0]+'='+t[1]);}

(二)利用forEach进行遍历

  对数组进行遍历:

      var arr=['1',2,4,121];

     arr.forEach(function(element,index,array)

{ console.log(element);}

)

   输出结果为:1  2  4  121

   对map进行操作:   

var m = new Map([[1, 'x'], [2, 'y'], [3, 'z']]);
m.forEach(function(value,key,map)
{console.log(value);});

  输出结果为:x y z

原文地址:https://www.cnblogs.com/yyn120804/p/6391506.html