乐闻世界logo
搜索文章和话题

如何从 js 数组中删除重复值

3个答案

1
2
3

在JavaScript中,从数组中删除重复值可以通过几种不同的方法实现。以下是一些常见的方法,每种方法都有其自身的优势。

1. 使用Set对象

Set是ES6中引入的一个新的数据结构,它允许你存储唯一值(重复的元素会被忽略)。我们可以利用这个特性来删除数组中的重复值。

javascript
const array = [1, 2, 2, 3, 4, 4, 5]; const uniqueArray = [...new Set(array)]; console.log(uniqueArray); // 输出: [1, 2, 3, 4, 5]

使用Set对象是最简洁的方法,代码易于理解,且性能良好。

2. 使用filter方法

Array.prototype.filter方法可以用来遍历数组并返回一个新数组,包含所有通过测试函数的元素。我们可以利用这个方法来筛选出第一次出现的元素,从而达到去重的效果。

javascript
const array = [1, 2, 2, 3, 4, 4, 5]; const uniqueArray = array.filter((item, index, arr) => arr.indexOf(item) === index); console.log(uniqueArray); // 输出: [1, 2, 3, 4, 5]

这种方法不需要任何外部库或特定的语言特性,因此适用于老版本的JavaScript环境。

3. 使用reduce方法

Array.prototype.reduce方法对数组中的每个元素执行一个由您提供的“reducer”函数,将其结果汇总为单个返回值。我们可以用它来构建一个不包含重复值的数组。

javascript
const array = [1, 2, 2, 3, 4, 4, 5]; const uniqueArray = array.reduce((acc, current) => { if (acc.indexOf(current) === -1) { acc.push(current); } return acc; }, []); console.log(uniqueArray); // 输出: [1, 2, 3, 4, 5]

这种方法给了我们更多的控制力,但它的效率可能不如使用Set对象。

4. 使用forEach方法和辅助对象

我们也可以使用forEach遍历数组,并使用一个辅助对象(或者Map)来记录已经出现过的值。

javascript
const array = [1, 2, 2, 3, 4, 4, 5]; let uniqueObject = {}; const uniqueArray = []; array.forEach((item) => { if (!uniqueObject[item]) { uniqueArray.push(item); uniqueObject[item] = true; } }); console.log(uniqueArray); // 输出: [1, 2, 3, 4, 5]

这种方法的效率也比较高,但是代码稍微复杂一些,并且需要额外的空间来存储辅助对象。


每种方法都有其适用场景,选择哪一种取决于具体需求、代码的可读性以及对旧版JavaScript的支持需求。例如,如果你正在编写一个需要在老版本浏览器上运行的应用程序,你可能需要避免使用Setfilter,而是选择for循环或其他ES5兼容的方法。如果你的环境支持ES6,那么使用Set可能是最简单和最直观的方式。

2024年6月29日 12:07 回复

.filter使用数组和函数的单行版本.indexOf

shell
arr = arr.filter(function (value, index, array) { return array.indexOf(value) === index; });

ES6方法

shell
arr = arr.filter((value, index, array) => array.indexOf(value) === index )
2024年6月29日 12:07 回复

在JavaScript中,可以使用多种方法从数组中删除重复的值。

  1. 使用 Set Set 是一种允许你存储任何类型唯一值的集合。因此,你可以利用 Set 来轻松去重。

    javascript
    const array = [1, 2, 2, 3, 4, 4, 5]; const uniqueArray = [...new Set(array)]; console.log(uniqueArray); // [1, 2, 3, 4, 5]
  2. 使用 filter() filter() 方法可以用来创建一个新数组,这个新数组由通过所提供函数测试的所有元素组成。这里我们可以检查当前元素的索引是否是它首次出现的索引。

    javascript
    const array = [1, 2, 2, 3, 4, 4, 5]; const uniqueArray = array.filter((item, index, arr) => arr.indexOf(item) === index); console.log(uniqueArray); // [1, 2, 3, 4, 5]
  3. 使用 reduce() reduce() 方法对数组中的每个元素执行一个由你提供的“reducer”回调函数,将其结果汇总为单个返回值。你可以使用它来构建一个不包含重复项的新数组。

    javascript
    const array = [1, 2, 2, 3, 4, 4, 5]; const uniqueArray = array.reduce((accumulator, current) => { if (accumulator.indexOf(current) === -1) { accumulator.push(current); } return accumulator; }, []); console.log(uniqueArray); // [1, 2, 3, 4, 5]
  4. 使用 forEach() 和 includes() 你可以遍历数组并使用 includes() 方法检查结果数组中是否已经包含了当前项。

    javascript
    const array = [1, 2, 2, 3, 4, 4, 5]; const uniqueArray = []; array.forEach(item => { if (!uniqueArray.includes(item)) { uniqueArray.push(item); } }); console.log(uniqueArray); // [1, 2, 3, 4, 5]

以上是一些常见的方法来去除数组中的重复项。你可以根据实际情况选择最适合你的方法。通常情况下,使用 Set 是最简单快捷的方式。

2024年6月29日 12:07 回复

你的答案