Is there any way to modify a Set data structure in iteration (ECMAScript 6)? -
the set
object in es6 has foreach
method, array
object does. there way modify values when iterating on set
object using foreach
method?
for example:
// array object in es5 can modified in iteration var array = [1, 2, 3]; array.foreach(function(int, idx, a) { a[idx] = int * int; }); array; // => [1, 4, 9]
but when iterating on set object,
// set not updated var set = new set([1, 2, 3]); set.foreach(function(val1, val2, s) { val2 = val1 * val1; }) set; // => [1, 2, 3]
is there way achieve same effect array
object?
i'd do
var set = new set([1, 2, 3]); set = new set(array.from(set, val => val * val));
to make new set new values, , replace old. mutating set iterate on bad idea , seems it's easy avoid in usecase.
Comments
Post a Comment