Skip to content

Instantly share code, notes, and snippets.

@wangwen1220
Created January 26, 2014 06:59
Show Gist options
  • Select an option

  • Save wangwen1220/8629514 to your computer and use it in GitHub Desktop.

Select an option

Save wangwen1220/8629514 to your computer and use it in GitHub Desktop.
JS: 去掉Javscript数组中的重复元素 | removeDuplicates
// 去掉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