JS: Set.prototype.forEach
(new in JS: ECMAScript 2015)
mySet.forEach(f)
-
- Apply function f to all values in set mySet.
- Does not modify the set object.
- Return undefined.
- f is passed 3 arguments, x y z.
- The x is the current value in the set, y is the same as x, z is the set being iterated.
- The reason the first two arguments are the same is because to keep compatible in form with the forEach method of Array.
const xx = new Set([3, 4, 5]); // apply a function to each value const yy = xx.forEach((x) => { console.log(x + 10); return x + 10; }); /* 13 14 15 */ // does not modify the variable console.log(xx); // Set(3) { 3, 4, 5 } // returns undefined console.log(yy); // undefined
// 3 args form of f in forEach on a set object const xx = new Set([3, 4, 5]); // apply a function to each value xx.forEach(function (aa, bb, cc) { console.log([aa, bb, cc]); }); /* [ 3, 3, Set(3) { 3, 4, 5 } ] [ 4, 4, Set(3) { 3, 4, 5 } ] [ 5, 5, Set(3) { 3, 4, 5 } ] */
mySet.forEach(f, thisArg)
-
Use thisArg for this (binding) of f. Default to
undefined
.