日期:2014-05-16 浏览次数:20656 次
var list=["iPod","iMac","Macbook Pro","iMac","iPhone","iPad","iMac","iPod"];
/*
list数组中iMac重复2次,iPod重复1次;
如何删除数组中重复的值?
*/
function unique(arr) {
var result = [], hash = {};
for (var i = 0, elem; (elem = arr[i]) != null; i++) {
if (!hash[elem]) {
result.push(elem);
hash[elem] = true;
}
}
return result;
}
var list='["iPod","iMac","Macbook Pro","iMac","iPhone","iPad","iMac","iPod"]';
alert(eval('(' +list.replace(/,/g,"").replace(/("[^"].*[^"]")(?=.*\1)/g, "").replace(/""/g, '","')+')' ));
var list=["iPod","iMac","Macbook Pro","iMac","iPhone","iPad","iMac","iPod"];
list=("@"+list.join("#@")+"#").replace(/(@.*?#)(?=.*\1)/ig,"").slice(1,-1).split("#@");
alert(list);