JSON数组去重

/*
* JSON数组去重
* @param: [array] json Array
* @param: [string] 唯一的key名,根据此键名进行去重
*/
function uniqueArray(array, key,mergekey){
var result = [array[0]];
for(var i = 1; i < array.length; i++){
var item = array[i];
var repeat = false;
for (var j = 0; j < result.length; j++) {
if (item[key] == result[j][key]) {
repeat = true;
result[j][mergekey]=result[j][mergekey]+','+item[mergekey];
break;
}
}
if (!repeat) {
result.push(item);
}
}
//console.log(result);
return result;


}
posted @ 2018-01-05 16:53  泥娃拉拉  阅读(1147)  评论(0编辑  收藏  举报