请给Array本地对象增加一个原型方法,它的用途是删除数组条目中重复的条目(可能有多个),返回值是一个仅包含被删除的重复条目的新数组。
var hashCode = function(element){
return element.sort().toSource();
}
Array.prototype.dell = function(hashCode){
var deleList = [];
var obj = {};
do {
var ele = this.pop();
var key = hashCode(ele);
if (obj[key]) {
deleList.push(ele);
}
else {
obj[key] = ele;
}
}
while (this.length > 0);
for (var key in obj) {
this.push(obj[key]);
}
return deleList;
}
var list = [[3, 1], [1, 2], [1, 3]]
expect([[1, 3]]).to(equal, list.dell(hashCode));
expect([[1, 2], [1, 3]].sort()).to(equal, list.sort());