ECMAScript6(es6)实现并集(union)、交集(intersection)、差集(difference)

WEB开发 | 2018-03-15 12:47:28 | 阅读 1566 次 | 评论(1)

并集(union)

let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let union = new Set([...a, ...b]);          // {1,2,3,4}

交集(intersection)

let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let intersection = new Set([...a].filter(x => b.has(x)));       // {2,3}

差集(difference)

let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let difference = new Set([...a].filter(x => !b.has(x)));        // {1}

参考:http://2ality.com/2015/01/es6-set-operations.html



文章评论,共1条
Image
1楼: 粗顺滑权 发表于 2019-05-23 20:40   回复
游客请输入验证码
浏览1889159次
文章归档
最新评论
  • 神犇dengyuhy:语文很好
  • 猫云:学习 学习