如何在 JavaScript 中映射/减少/过滤集合? [英] How to map/reduce/filter a Set in JavaScript?

查看:22
本文介绍了如何在 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 来完成 mapreduce?

解决方案

一种简便的方法是通过 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 中映射/减少/过滤集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆