Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

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

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 SO that looks almost exactly like it, but it doesn't fail.(我在SO上找到了另一个脚本 ,看起来几乎完全一样,但是它不会失败。) 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; } More answers from duplicate question:(来自重复问题的更多答案:) Remove Duplicates from JavaScript Array(从JavaScript数组中删除重复项) Similar question:(类似的问题:) Get all values with more than one occurrence (ie: not unique) in an array(获取数组中出现多次(即不唯一)的所有值)   ask by Mottie translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:(使用JavaScript 1.6 / ECMAScript 5,您可以通过以下方式使用Array的本机filter方法来获取具有唯一值的数组:)

function onlyUnique(value, index, self) { return self.indexOf(value) === index; } // usage example: var a = ['a', 1, 'a', 2, '1']; var unique = a.filter( onlyUnique ); // returns ['a', 1, 2, '1'] The native method filter will loop through the array and leave only those entries that pass the given callback function onlyUnique .(本地方法filter将遍历数组,仅保留那些通过给定回调函数onlyUnique 。) onlyUnique checks, if the given value is the first occurring.(onlyUnique检查给定值是否是第一个出现。) If not, it must be a duplicate and will not be copied.(如果不是,它必须是重复的,并且不会被复制。) This solution works without any extra library like jQuery or prototype.js.(此解决方案无需任何额外的库(如jQuery或prototype.js)即可工作。) It works for arrays with mixed value types too.(它也适用于具有混合值类型的数组。) 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 .(对于旧的浏览器(<IE9),不支持原生的方法filterindexOf你可以找到MDN文档中的变通过滤器的indexOf 。) If you want to keep the last occurrence of a value, simple replace indexOf by lastIndexOf .(如果要保留最后一次出现的值,只需将indexOf替换为lastIndexOf 。) With ES6 it could be shorten to this:(使用ES6可以简化为:) // usage example: var myArray = ['a', 1, 'a', 2, '1']; var unique = myArray.filter((v, i, a) => a.indexOf(v) === i); // unique is ['a', 1, 2, '1'] Thanks to Camilo Martin for hint in comment.(感谢Camilo Martin的评论提示。) ES6 has a native object Set to store unique values.(ES6有一个本机对象Set用于存储唯一值。) To get an array with unique values you could do now this:(要获得具有唯一值的数组,您现在可以执行以下操作:) var myArray = ['a', 1, 'a', 2, '1']; let unique = [...new Set(myArray)]; // unique is ['a', 1, 2, '1'] The constructor of Set takes an iterable object, like Array, and the spread operator ... transform the set back into an Array.(Set的构造函数采用一个可迭代的对象(例如Array),并且散布运算符...将集合转换回Array。) Thanks to Lukas Liese for hint in comment.(感谢Lukas Liese的评论提示。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...