码迷,mamicode.com
首页 > 编程语言 > 详细

ES6 利用 Set 数组去重法

时间:2018-03-05 12:54:31      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:gpo   例子   重复   成员   ons   es6   for   body   set   

例子:

  const set = new Set();

  [2, 3, 5, 4, 5, 2, 2].forEach(x => set.add(x) );

  const arr = [...set];

  console.log(arr);  // [ 2, 3, 5, 4 ]

稍做一下解释:

  因为,ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值。

  所以,可以做出这样的方法;

ES6 利用 Set 数组去重法

标签:gpo   例子   重复   成员   ons   es6   for   body   set   

原文地址:https://www.cnblogs.com/cnblogs-jcy/p/8508318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!