Created
January 26, 2014 06:59
-
-
Save wangwen1220/8629514 to your computer and use it in GitHub Desktop.
JS: 去掉Javscript数组中的重复元素 | removeDuplicates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 去掉Javscript数组中的重复元素 | |
| function removeDuplicates(arr) { | |
| var temp = {}; | |
| for (var i = 0; i < arr.length; i++) | |
| temp[arr[i]] = true; | |
| var r = []; | |
| for (var k in temp) | |
| r.push(k); | |
| return r; | |
| } | |
| //用法 | |
| var fruits = ['apple', 'orange', 'peach', 'apple', 'strawberry', 'orange']; | |
| var uniquefruits = removeDuplicates(fruits); | |
| //输出 uniquefruits ['apple', 'orange', 'peach', 'strawberry']; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment