如何在 Javascript 中对地图键使用 .map()
How to use .map() over Map keys in Javascript
使用 Map 内置的 Javascript 时,如何使用 .map() 遍历键?
我知道 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);
}
但是这段代码会失败:
map.keys().map(key => {
console.log(key);
});
您可以使用 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);
});
希望对您有所帮助!
实际上是Array.prototype.map
,它是为数组定义的,所以使用Array.from
将keys
转换为数组,然后使用map
:
const map = new Map();
map.set(0, 'Zero');
map.set(1, 'One');
map.set(2, 'Two');
console.log(...Array.from(map.keys()).map(key => {
return key ** 2; // square the keys
}));
您最好直接使用 Array.from
映射函数以避免创建临时数组:
Array.from(map.keys(), k => console.log(k))
另一个更冗长但有用的选项是在迭代器原型上重新定义数组迭代方法,从而使它们自动可用于所有迭代器:
// 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.keys()
returns 一个迭代器,您可以使用 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);
})
map.forEach((_,key) => console.log(key));
正如其他答案指出的那样,map
是一种 Array
方法,因此您必须先将 map.keys()
返回的可迭代对象转换为数组。这比使用 for
循环效率低,因为不需要转换。在我写的 library 中,有一个函数可以在不创建数组的情况下将一个可迭代对象映射到另一个可迭代对象:
export const mapIterable = <From, To>(project: (value: From) => To) =>
function* (iterable: Iterable<From>): IterableIterator<To> {
for (const value of iterable) {
yield project(value);
}
};
使用 Map 内置的 Javascript 时,如何使用 .map() 遍历键?
我知道 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);
}
但是这段代码会失败:
map.keys().map(key => {
console.log(key);
});
您可以使用 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);
});
希望对您有所帮助!
实际上是Array.prototype.map
,它是为数组定义的,所以使用Array.from
将keys
转换为数组,然后使用map
:
const map = new Map();
map.set(0, 'Zero');
map.set(1, 'One');
map.set(2, 'Two');
console.log(...Array.from(map.keys()).map(key => {
return key ** 2; // square the keys
}));
您最好直接使用 Array.from
映射函数以避免创建临时数组:
Array.from(map.keys(), k => console.log(k))
另一个更冗长但有用的选项是在迭代器原型上重新定义数组迭代方法,从而使它们自动可用于所有迭代器:
// 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.keys()
returns 一个迭代器,您可以使用 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);
})
map.forEach((_,key) => console.log(key));
正如其他答案指出的那样,map
是一种 Array
方法,因此您必须先将 map.keys()
返回的可迭代对象转换为数组。这比使用 for
循环效率低,因为不需要转换。在我写的 library 中,有一个函数可以在不创建数组的情况下将一个可迭代对象映射到另一个可迭代对象:
export const mapIterable = <From, To>(project: (value: From) => To) =>
function* (iterable: Iterable<From>): IterableIterator<To> {
for (const value of iterable) {
yield project(value);
}
};