Using map() on an iterator

后端 未结 8 1892
误落风尘
误落风尘 2021-02-01 11:59

Say we have a Map: let m = new Map();, using m.values() returns a map iterator.

But I can\'t use forEach() or map() o

8条回答
  •  自闭症患者
    2021-02-01 12:04

    Other answers here are... Weird. They seem to be re-implementing parts of the iteration protocol. You can just do this:

    function* mapIter(iterable, callback) {
      for (let x of iterable) {
        yield callback(x);
      }
    }
    

    and if you want a concrete result just use the spread operator ....

    [...iterMap([1, 2, 3], x => x**2)]
    

提交回复
热议问题