从阵列中完全删除重复的项目 [英] Completely removing duplicate items from an array

查看:77
本文介绍了从阵列中完全删除重复的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们假设我有;

var array = [1,2,3,4,4,5,5];

我希望它是;

var newArray = [1,2,3];

我想完全删除重复项,而不是将它们保留为唯一值。有没有办法通过reduce方法实现呢?

I want to remove the duplicates completely rather than keeping them as unique values. Is there a way achieve that through reduce method ?

推荐答案

你可以使用 Array#filter Array #indexOf Array#lastIndexOf 并仅返回共享相同索引的值。

You could use Array#filter with Array#indexOf and Array#lastIndexOf and return only the values which share the same index.

var array = [1, 2, 3, 4, 4, 5, 5],
    result = array.filter(function (a, _, aa) {
        return aa.indexOf(a) === aa.lastIndexOf(a);
    });

console.log(result);

这篇关于从阵列中完全删除重复的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆