日期:2014-05-16  浏览次数:20656 次

如何删除数组中重复的值?
本帖最后由 mycggo 于 2014-05-07 12:46:59 编辑
比如:

var list=["iPod","iMac","Macbook Pro","iMac","iPhone","iPad","iMac","iPod"];
/*
list数组中iMac重复2次,iPod重复1次;
如何删除数组中重复的值?
*/

------解决方案--------------------
http://www.1008a.com/post/277.html
------解决方案--------------------
$.unique(["iPod","iMac","Macbook Pro","iMac","iPhone","iPad","iMac","iPod"])
------解决方案--------------------
list =list.sort();
$.unique(list);

------解决方案--------------------
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, '","')+')' ));

------解决方案--------------------
http://borninsummer.com/blog/2013/12/09/javascript-array-unique/
------解决方案--------------------
引用:
比如:

var list=["iPod","iMac","Macbook Pro","iMac","iPhone","iPad","iMac","iPod"];
/*
list数组中iMac重复2次,iPod重复1次;
如何删除数组中重复的值?
*/

var list=["iPod","iMac","Macbook Pro","iMac","iPhone","iPad","iMac","iPod"];
list=("@"+list.join("#@")+"#").replace(/(@.*?#)(?=.*\1)/ig,"").slice(1,-1).split("#@");
alert(list);