关于javascript:删除重复的数组项

remove duplicated array item

本问题已经有最佳答案,请猛点这里访问。
1
2
3
4
5
var arr = ["Chandelier","Big Girls Cry","Burn the Pages","Eye of the Needle","Hostage","Straight for the Knife","Fair Game","Elastic Heart","Free the Animal","Fire Meet Gasoline","Cellophane","Dressed In Black","Chandelier","Elastic Heart","Chandelier","Chandelier","Elastic Heart","Elastic Heart","Big Girls Cry","Big Girls Cry"];

 $.each(arr, function(i,obj){
console.log(obj);
});

如何确保我的数组是唯一的?


Array#filter()和临时对象。

1
2
3
4
5
6
7
8
9
var arr = ["Chandelier","Big Girls Cry","Burn the Pages","Eye of the Needle","Hostage","Straight for the Knife","Fair Game","Elastic Heart","Free the Animal","Fire Meet Gasoline","Cellophane","Dressed In Black","Chandelier","Elastic Heart","Chandelier","Chandelier","Elastic Heart","Elastic Heart","Big Girls Cry","Big Girls Cry"],
    unique = arr.filter(function (a) {
        if (!this[a]) {
            this[a] = true;
            return true;
        }
    }, {});    
   
document.write('[cc lang="javascript"]' + JSON.stringify(unique, 0, 4) + '

’;>pre>