获取 JavaScript 数组中的所有唯一值(删除重复项) [英] Get all unique values in a JavaScript array (remove duplicates)

查看:37
本文介绍了获取 JavaScript 数组中的所有唯一值(删除重复项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数字,需要确保它们是唯一的.我在互联网上找到了下面的代码片段,它运行良好,直到数组中有一个零.我在 Stack Overflow 上发现了这个其他脚本,它看起来几乎一模一样,但并没有失败.

I have an array of numbers that I need to make sure are unique. I found the code snippet below on the internet and it works great until the array has a zero in it. I found this other script here on Stack Overflow that looks almost exactly like it, but it doesn't fail.

所以为了帮助我学习,谁能帮我确定原型脚本哪里出错了?

So for the sake of helping me learn, can someone help me determine where the prototype script is going wrong?

Array.prototype.getUnique = function() {
 var o = {}, a = [], i, e;
 for (i = 0; e = this[i]; i++) {o[e] = 1};
 for (e in o) {a.push (e)};
 return a;
}

来自重复问题的更多答案:

  • 从 JS 数组中删除重复值
  • 推荐答案

    使用 JavaScript 1.6/ECMAScript 5 你可以使用原生的 filter 数组的方法通过以下方式获取具有唯一值的数组:

    With JavaScript 1.6 / ECMAScript 5 you can use the native filter method of an Array in the following way to get an array with unique values:

    function onlyUnique(value, index, self) {
      return self.indexOf(value) === index;
    }
    
    // usage example:
    var a = ['a', 1, 'a', 2, '1'];
    var unique = a.filter(onlyUnique);
    
    console.log(unique); // ['a', 1, 2, '1']

    本地方法 filter 将遍历数组并只留下那些通过给定回调函数 onlyUnique 的条目.

    The native method filter will loop through the array and leave only those entries that pass the given callback function onlyUnique.

    onlyUnique 检查给定的值是否是第一个出现的值.如果不是,它必须是重复的,不会被复制.

    onlyUnique checks, if the given value is the first occurring. If not, it must be a duplicate and will not be copied.

    此解决方案无需任何额外的库,如 jQuery 或prototype.js.

    This solution works without any extra library like jQuery or prototype.js.

    它也适用于具有混合值类型的数组.

    It works for arrays with mixed value types too.

    对于不支持本地方法 filterindexOf 的旧浏览器 (<ie9),您可以在 MDN 文档中找到 filterindexOf.

    For old Browsers (<ie9), that do not support the native methods filter and indexOf you can find work arounds in the MDN documentation for filter and indexOf.

    如果您想保留最后一次出现的值,只需将 indexOf 替换为 lastIndexOf.

    If you want to keep the last occurrence of a value, simple replace indexOf by lastIndexOf.

    使用 ES6 可以缩短为:

    With ES6 it could be shorten to this:

    // usage example:
    var myArray = ['a', 1, 'a', 2, '1'];
    var unique = myArray.filter((v, i, a) => a.indexOf(v) === i);
    
    console.log(unique); // unique is ['a', 1, 2, '1']

    感谢 Camilo Martin 的评论提示.

    ES6 有一个原生对象 Set 存储唯一值.要获得具有唯一值的数组,您现在可以这样做:

    ES6 has a native object Set to store unique values. To get an array with unique values you could now do this:

    var myArray = ['a', 1, 'a', 2, '1'];
    
    let unique = [...new Set(myArray)];
    
    console.log(unique); // unique is ['a', 1, 2, '1']

    Set 的构造函数接受一个可迭代对象,如 Array,而扩展运算符 ... 将集合转换回一个 Array.感谢 Lukas Liese 的评论提示.

    The constructor of Set takes an iterable object, like Array, and the spread operator ... transform the set back into an Array. Thanks to Lukas Liese for hint in comment.

    这篇关于获取 JavaScript 数组中的所有唯一值(删除重复项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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