1. 首页
  2. 软件开发
  3. 去除数组重复成员的方法

去除数组重复成员的方法

  • 发布于 2024-09-26
  • 1 次阅读

方法 1 扩展运算符和 Set 结构相结合,就可以去除数组的重复成员

// 去除数组的重复成员
[...new Set([1, 2, 2, 3, 4, 5, 5])];
// [1, 2, 3, 4, 5]

方法 2

function dedupe(array) {
    return Array.from(new Set(array));
}
dedupe([1, 1, 2, 3]); // [1, 2, 3]

方法 3(ES5)

function unique(arry) {
    const temp = [];
    arry.forEach(e => {
        if (temp.indexOf(e) == -1) {
            temp.push(e);
        }
    });

    return temp;
}


原文链接: https://blog.csdn.net/hexadecimal_001/article/details/139693898