简体   繁体   English

如何在 Javascript 中的 Map 键上使用 .map()

[英]How to use .map() over Map keys in Javascript

When using the Javascript built in Map , how do you use .map() to iterate over the keys?使用Map内置的 Javascript 时,如何使用 .map() 迭代键?

I know the for...of can be used as shown below:我知道 for...of 可以如下所示使用:

const map = new Map();

map.set(0, 'Zero');
map.set(1, 'One');
map.set(2, 'Two');

for (let key of map.keys()) {
  console.log(key);
}

But this code will fail:但是这段代码会失败:

map.keys().map(key => {
  console.log(key);
});

It's Array.prototype.map actually, it's defined for arrays, so use Array.from to convert the keys to an array and then use map :它实际上是Array.prototype.map ,它是为数组定义的,因此使用Array.fromkeys转换为数组,然后使用map

 const map = new Map(); map.set(0, 'Zero'); map.set(1, 'One'); map.set(2, 'Two'); Array.from(map.keys()).map(key => { console.log(key); });

You can use Array.from() :您可以使用Array.from()

 const map = new Map(); map.set(0, 'Zero'); map.set(1, 'One'); map.set(2, 'Two'); Array.from(map.keys()).map(key => { console.log(key); });

Hopefully that helps!希望这有帮助!

Map.keys() returns an iterator you can spread the iterator using spread syntax Map.keys()返回一个迭代器,您可以使用spread syntax扩展迭代器

 const map = new Map(); map.set(0, 'Zero'); map.set(1, 'One'); map.set(2, 'Two'); [...map.keys()].forEach(key => { console.log(key); })

You might be better off using Array.from mapping function directly to avoid creating a temporary array:您最好直接使用Array.from映射函数来避免创建临时数组:

Array.from(map.keys(), k => console.log(k))

Another, more verbose, but helpful option would be to redefine array iteration methods on the iterator prototype, thus making them automatically available for all iterators:另一个更冗长但有用的选项是在迭代器原型上重新定义数组迭代方法,从而使它们自动可用于所有迭代器:

 // http://www.ecma-international.org/ecma-262/7.0/#sec-%iteratorprototype%-object const IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())); Object.defineProperties(IteratorPrototype, { forEach: { value: function (fn) { let n = 0; for (let x of this) { fn(x, n++, this); } }, enumerable: false }, map: { value: function (fn) { let n = 0, a = []; for (let x of this) { a.push(fn(x, n++, this)); } return a; }, enumerable: false }, reduce: { value: function (fn, init) { let n = 0; if (arguments.length === 1) { init = this.next().value; } for (let x of this) { init = fn(init, x, n++, this); } return init; }, enumerable: false }, }); ///// const map = new Map(); map.set('a', 'Zero'); map.set('b', 'One'); map.set('c', 'Two'); map.keys().map(console.log) console.log(map.values().reduce((o, k) => o + '/' + k)); function* it() { yield 'x'; yield 'y'; yield 'z'; } it().map(x => console.log(x))

map.forEach((_,key) => console.log(key));

As other answers point out, map is an Array method, so you have to convert the iterable returned by map.keys() to an array first.正如其他答案指出的那样, map是一个Array方法,因此您必须map.keys()返回的可迭代对象转换为数组。 This is less efficient than using a for loop because there the conversion isn't required.这比使用for循环效率低for因为不需要转换。 In a library I've written there is a function that maps an iterable to another iterable without creating an array:在我编写的库中,有一个函数可以将一个可迭代对象映射到另一个可迭代对象,而无需创建数组:

export const mapIterable = <From, To>(project: (value: From) => To) =>
  function* (iterable: Iterable<From>): IterableIterator<To> {
    for (const value of iterable) {
      yield project(value);
    }
  };

You'd normally use with a ponyfill for the pipeline operator .您通常会为管道操作员使用小马填充

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM