JavaScript去除数组中的重复性

Array.prototype.unique = function () {
var res = [], hash = {};
for (var i = 0, elem; (elem = this[i]) != null; i++) {
if (!hash[elem]) {
res.push(elem);
hash[elem] = true;
}
}
return res;
}

使用

var arr = new Array(1, 3, 4, 5, 3);
arr = arr.unique();
alert(arr.join(",")) //输出1,3,4,5
上一篇:[cocos2d-x]HelloWorldDemo


下一篇:Hongcow Buys a Deck of Cards CodeForces - 744C (状压)