之前写的一直有问题,不知道在多数据情况会出BUG。都一年多了我的天,没人反馈。(误人子弟小火鸡)
同时感谢下面两位评论的朋友提醒。(已更改)
var arr = [{ uuid: '1', name: '张三' },
{ uuid: '2', name: '李四' },
{ uuid: '3', name: '王五' },
{ uuid: '2', name: '李四' },
{ uuid: '1', name: '张三' },
{ uuid: '1', name: '张三' }]
function RemoveArr(arr) {
for (var i = 0; i < arr.length; i++) {
for (var j = i+1; j < arr.length; ) {
if(arr[i].uuid==arr[j].uuid){
arr.splice(j,1)
}else{
j++
}
}
}
return arr
}
console.log(RemoveArr(arr))
///
新方法!
function RemoveArr(arr) {
var arr1 = []
for (var i = 0; i < arr.length; i++) {
if (arr1.map(x => x.uuid).indexOf(arr[i].uuid) == -1) {
arr1.push(arr[i])
}
}
return arr1
}
//
function jsonUniq(arrjson) {
let arr1 = [arrjson[0]];
arrjson.forEach(function (item1, idx1) {
let flag = false;
arr1.forEach(function (item2, idx2) {
if (item1.uuid == item2.uuid) {
flag = true;
return;
}
})
if (!flag ) {
arr1.push(item1)
}
})
return arr1;
}