Skip to content

Instantly share code, notes, and snippets.

@jakeauyeung
Created December 11, 2014 06:34
Show Gist options
  • Save jakeauyeung/b7d6e77fc8d1f11bcb45 to your computer and use it in GitHub Desktop.
Save jakeauyeung/b7d6e77fc8d1f11bcb45 to your computer and use it in GitHub Desktop.
数组合并去重
var _user01 = [
{'id': '11', 'name': 'fame'},
{'id': '11'},
{'id': '22'}
]
var _user02 = [
{'id': '11', 'name': 'fame', 'sex': 'famle'},
{'id': '11', 'sesd':'2343'},
{'id': '22'},
{'id': '2332','name': 'se'}
]
Array.prototype.unique = function(source,compareFn){
var _length =source.length,
result =source.slice(0), //clone source
i,_item;
// if compareFn is not function we will build one
if('functrion' != typeof compareFn){
compareFn = function(item1,item2){
return item1 === item2;
};
}
//we use double circulation to compare the items
while (--_length >0){
_item = result[_length]; // start from the last
i = _length;
while(i --){
if(compareFn(_item,result[i])){
result.splice(_length,1);
break;
}
}
}
return result;
};
console.log(_user01.unique(_user02))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment