理由es6实现数组去重

以下是三种方法从数组里去重,并且返回唯一的值。我最喜欢的方式是使用Set,因为它是最短最简单的。

const array = [5, 2, 4, 5, 3];
console.log([...new Set(array)])
console.log(array.filter((item, index) => array.indexOf(item) === index))
console.log(array.reduce((unique, item) => unique.includes(item) ? unique: [...unique, item], []))
// result:  [5, 2, 4, 3]
使用Set

让我开始解释Set是什么吧:

Set是由es6引入的一种新的数据对象,由于Set只允许你存储唯一值,所以当传递进去一个数组的时候,它将会移除任何重复的值。

好啦,然我们回到我们的代码中来看下到底都发生了什么。实际上他进行了以下的操作:

首先,我们创建了新的Set并且把数组当作入参传递了进去,由于Set仅仅允许唯一值,所以所有重复值将会被移除。
现在重复值已经消失了,我们将会利用…把它重新转为数组。

const array = [5, 2, 4, 5, 3];
const set = new Set(array)
const newArr = [...set]
console.log(newArr)
// result:  [5, 2, 4, 3]

使用Array.from()函数来吧Set转为数组

另外呢,你也可以使用Array.from()来吧Set转为数组。

const array = [5, 2, 4, 5, 3];
const set = new Set(array)
const newArr = Array.from(set)
console.log(newArr)
// result:  [5, 2, 4, 3]
使用filter

为了理解这个选项,让我们来看看这两个方法都做了什么:indexOf和filter

indexOf()

indexOf()返回我们从数组里找到的第一个元素的索引。

const array = [5, 2, 4, 5, 3];
console.log(array.indexOf(5))  // 0
console.log(array.indexOf(2))  // 1
console.log(array.indexOf(8))  // -1

filter

filter()函数会根据我们提供的条件创建一个新的数组。换一种说法,如果元素通过并且返回true,它将会包含在过滤后的数组中,如果有元素失败并且返回false,那么他就不会包含在过滤后的数组中。

我们逐步看看在每次循环数组的时候都发生了什么。

const array = [5, 2, 4, 5, 3];
array.filter((item, index) => {
  console.log(item, index, array.indexOf(item), array.indexOf(item) === index)
  return array.indexOf(item) === index
})
//输出
// 5 0 0 true
// 2 1 1 true
// 4 2 2 true
// 5 3 0 false
// 3 4 4 true

上面输出的代码见注释。重复的元素不再于indexOf相匹配,所以在这些情况下,它的结果将会是false并且将不会被包含进过滤后的值当中。

检索重复的值

我们也可以在数组中利用filter()函数来检索重复的值。我们只需要像这样简单的调整下代码:

const array = [5, 2, 4, 5, 3];
array.filter((item, index) => {
  console.log(item, index, array.indexOf(item), array.indexOf(item) !== index)
  return array.indexOf(item) !== index
})
//输出
// 5 0 0 false
// 2 1 1 false
// 4 2 2 false
// 5 3 0 true
// 3 4 4 false
使用reduce()函数

reduce()函数用于减少数组的元素并根据你传递过去的reducer函数,把他们最终合并到一个最终的数组中,

在这种情况下,我们的reducer()函数我们最终的数组是否包含了这个元素,如果不包含,就吧他放进最终的数组中,反之则跳过这个元素,最后再返回我们最终的元素。

reduce()函数理解起来总是有那么一点费劲,所以呢,咱们现在看下他是怎么运行的。

const array = [5, 2, 4, 5, 3];
array.reduce((unique, item) => {
  console.log(item, unique, unique.includes(item), unique.includes(item) ? unique: [...unique, item])
  return unique.includes(item) ? unique: [...unique, item]
}, [])
//输出
// 5 []          false   [5]
// 2 [5]         false   [5, 2]
// 4 [5, 2]      false   [5, 2, 4]
// 5 [5, 2, 4]   true    [5, 2, 4]
// 3 [5, 2, 4]   false   [5, 2, 4, 3]

转载自:https://segmentfault.com/a/1190000017902898

ES6中,有多种实现数组的方法: - **使用Set和扩展运算符**:Set是ES6提供的一种新的数据结构,它类似于数组,但成员的值都是唯一的。扩展运算符(`...`)可以将Set对象展开为数组。示例代码如下: ```javascript const arr = [1, 2, 3, 4, 4, 5, 5, 6, 6]; const uniqueArr = [...new Set(arr)]; console.log(uniqueArr); // [1, 2, 3, 4, 5, 6] ``` 这种方法简洁高效,是ES6中最常用的数组方法之一[^1][^2][^3]。 - **使用Set和`Array.from()`方法**:`new Set()`生成的是一个Set类型,而不是数组类型,`Array.from()`方法可以将类数组对象或可迭代对象转换为真正的数组。示例代码如下: ```javascript let arr = [1,1,1,2,2,3,3,3,4,4]; let set = new Set(arr); console.log(set); let arr2 = Array.from(set); // 将类数组转化为真数组 console.log(arr2); ``` 这种方式同样可以实现普通数组,先通过`Set`复元素,再将`Set`对象转换为数组[^5]。 - **使用`filter()`与`findIndex()`给数组对象**:对于数组中包含对象的情况,可以使用`filter()`和`findIndex()`方法结合来实现。`filter()` 方法创建一个新数组,其包含通过所提供函数实现的测试的所有元素;`findIndex()`方法返回数组中满足提供的测试函数的第一个元素的索引。示例代码如下: ```javascript const arr = [ { id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 1, name: 'a' } ]; const uniqueArr = arr.filter((item, index, self) => { return self.findIndex(t => t.id === item.id) === index; }); console.log(uniqueArr); ``` 该方法通过`findIndex`找到满足条件的第一个元素的索引,然后通过`filter`过滤出索引等于当前元素索引的元素,从而实现对象数组[^4]。 - **使用`reduce()`**:`reduce()` 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。示例代码如下: ```javascript const arr = [1, 2, 2, 3, 4, 4, 5]; const uniqueArr = arr.reduce((acc, current) => { if (!acc.includes(current)) { acc.push(current); } return acc; }, []); console.log(uniqueArr); ``` 在这个例子中,`reduce`方法遍历数组,通过`includes`方法检查当前元素是否已经在累加器数组中,如果不在则添加到累加器中,最终返回后的数组 [^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值