使用ES6对数组进行去重操作

首先将数组转换为Set类型。

Array.from方法可以将 Set 结构转为数组。

const items = new Set([1, 2, 3, 4, 5]);
const array = Array.from(items); 
function dedupe(array) {
  return Array.from(new Set(array));
}

dedupe([1, 1, 2, 3]) // [1, 2, 3]

posted on 2018-01-26 11:24  王蒙Wmmm  阅读(163)  评论(0)    收藏  举报