The ES6 Standard introduces new
iterable
types,
Array
Map
and all of
Set
them belong to
iterable
types. a collection of 1.for of loops with a
iterable
type can be traversed by a new
for ... of
loop. var a = [' A ', ' B ', ' C '];
A.name = ' Hello ';
for (var x of a) {
alert (x); ' A ', ' B ', ' C '
}
for ... of
for ... in
What is the difference between loops and loops ? The for ... in loop is actually a property name for an object because of a history legacy problem. An array is actually an object, and its index of each element is treated as an attribute (for in loops include name, but the length property of the array is not included;)andFor : of loops He only loops the collection itself, returning the key-value pairs in the array instead of the attributes. var a = [' A ', ' B ', ' C '];a.name = ' Hello ';
for (Var x in a) {
alert (x); ' 0 ', ' 1 ', ' 2 ', ' name '
} However, a better approach is to use the built-in
iterable
forEach
method directly, which receives a function that automatically callbacks the function each time it iterates. Take
Array
for example: var a = [' A ', ' B ', ' C '];
A.foreach (function (element, index, array) {
Element: A value that points to the current element
Index: point to Current index
Array: points to the array object itself
alert (element);
});
Set
Array
Similar but
Set
no index, so the first two parameters of the callback function are the elements themselves: var s = new Set ([' A ', ' B ', ' C ']);
S.foreach (function (element, sameelement, set) {
alert (element);
});
Map
The callback function parameters are sequentially
value
,
key
and
map
themselves: var a = [' A ', ' B ', ' C '];
A.foreach (function (Element) {
alert (element);
});If you are not interested in certain parameters, they can be ignored because JavaScript function calls do not require parameters to be consistent. For example, you only need to obtain
Array
element
: var a = [' A ', ' B ', ' C '];
A.foreach (function (Element) {
alert (element);
});
Ecmascript6--iterable