可以使用Set数据结构来实现数组去重。

const arr = [1, 2, 3, 1, 2, 4, 5, 3];

const uniqueArr = Array.from(new Set(arr));

console.log(uniqueArr);
// Output: [1, 2, 3, 4, 5]

另外,也可以使用reduce()函数来进行数组去重。

const arr = [1, 2, 3, 1, 2, 4, 5, 3];

const uniqueArr = arr.reduce((accumulator, currentValue) => {
    if (!accumulator.includes(currentValue)) {
        accumulator.push(currentValue);
    }
    return accumulator;
}, []);

console.log(uniqueArr);
// Output: [1, 2, 3, 4, 5]

以上两种方法都可以实现数组去重,选择其中一种即可。

JavaScript数组去重:两种高效方法详解

原文地址: https://www.cveoy.top/t/topic/pdwm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录