Using map() on an iterator

后端 未结 8 1816
误落风尘
误落风尘 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:14

    There is a proposal, that is bringing multiple helper functions to Iterator: https://github.com/tc39/proposal-iterator-helpers (rendered)

    You can use it today by utilizing core-js:

    import { from as iterFrom } from "core-js-pure/features/iterator";
    
    // or if it's working for you:
    // import iterFrom from "core-js-pure/features/iterator/from";
    
    let m = new Map();
    
    m.set("13", 37);
    m.set("42", 42);
    
    const arr = iterFrom(m.values())
      .map((val) => val * 2)
      .toArray();
    
    // prints "[74, 84]"
    console.log(arr);
    

提交回复
热议问题