How to map/reduce/filter a Set in JavaScript?(如何在 JavaScript 中映射/减少/过滤集合?)
问题描述
有没有办法在 JavaScript 中 map
/reduce
/filter
/etc 一个 Set
或者我会必须自己写吗?
这里有一些合理的 Set.prototype
扩展
Set.prototype.map = function map(f) {var newSet = new Set();for (var v of this.values()) newSet.add(f(v));返回新集;};Set.prototype.reduce = function(f,initial) {var 结果 = 初始;for (var v of this) result = f(result, v);返回结果;};Set.prototype.filter = function filter(f) {var newSet = new Set();for (var v of this) if(f(v)) newSet.add(v);返回新集;};Set.prototype.every = function every(f) {for (var v of this) if (!f(v)) return false;返回真;};Set.prototype.some = function some(f) {for (var v of this) if (f(v)) 返回真;返回假;};
我们先来一组
let s = new Set([1,2,3,4]);
还有一些愚蠢的小功能
const times10 = x =>x * 10;常量添加 = (x,y) =>x + y;常量偶数 = x =>x % 2 === 0;
看看它们是如何工作的
s.map(times10);//=>设置 {10,20,30,40}s.reduce(添加, 0);//=>10s.filter(偶数);//=>设置 {2,4}s.every(偶数);//=>错误的s.some(偶数);//=>真的
<块引用>
这不是很好吗?是的,我也这么认为.将其与丑陋的迭代器用法进行比较
//呕吐让 newSet = new Set();for (let v in s) {newSet.add(times10(v));}
和
//barf让总和 = 0;for (let v in s) {总和 = 总和 + v;}
有没有更好的方法在 JavaScript 中使用 Set
来完成 map
和 reduce
?
一种简便的方法是通过 ES6 扩展运算符将其转换为数组.
那么你就可以使用所有的数组函数了.
const mySet = new Set([1,2,3,4]);[...mySet].reduce()
Is there any way to map
/reduce
/filter
/etc a Set
in JavaScript or will I have to write my own?
Here's some sensible Set.prototype
extensions
Set.prototype.map = function map(f) {
var newSet = new Set();
for (var v of this.values()) newSet.add(f(v));
return newSet;
};
Set.prototype.reduce = function(f,initial) {
var result = initial;
for (var v of this) result = f(result, v);
return result;
};
Set.prototype.filter = function filter(f) {
var newSet = new Set();
for (var v of this) if(f(v)) newSet.add(v);
return newSet;
};
Set.prototype.every = function every(f) {
for (var v of this) if (!f(v)) return false;
return true;
};
Set.prototype.some = function some(f) {
for (var v of this) if (f(v)) return true;
return false;
};
Let's take a little set
let s = new Set([1,2,3,4]);
And some stupid little functions
const times10 = x => x * 10;
const add = (x,y) => x + y;
const even = x => x % 2 === 0;
And see how they work
s.map(times10); //=> Set {10,20,30,40}
s.reduce(add, 0); //=> 10
s.filter(even); //=> Set {2,4}
s.every(even); //=> false
s.some(even); //=> true
Isn't that nice ? Yeah, I think so too. Compare that to the ugly iterator usage
// puke
let newSet = new Set();
for (let v in s) {
newSet.add(times10(v));
}
And
// barf
let sum = 0;
for (let v in s) {
sum = sum + v;
}
Is there any better way to accomplish map
and reduce
using a Set
in JavaScript?
A short-hand way to do it is to convert it to an array via the ES6 spread operator.
Then all the array functions are available to you.
const mySet = new Set([1,2,3,4]);
[...mySet].reduce()
这篇关于如何在 JavaScript 中映射/减少/过滤集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 JavaScript 中映射/减少/过滤集合?


- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 失败的 Canvas 360 jquery 插件 2022-01-01