案例根据多个键值对去重(如果yjId ejId sjId都一样,则去掉重复的)
数据是朋友发我的 emm 稍微删除一点吧
// 数据放下面了 比较乱
let key = ["yjId","ejId","sjId"];
let arrs = this.uniqueArray(arr,key)
// 思路
uniqueArray(array, key) {
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[0]] == result[j][key[0]] && item[key[1]] == result[j][key[1]] && item[key[2]] == result[j]
[key[2]]) {
repeat = true;
break;
}
}
if (!repeat) {
result.push(item);
}
}
return result;
},
// 简单优化
uniqueArrays(array, key) {
var result = [array[0]];
for (var i = 1; i < array.length; i++) {
var item = array[i];
var repeat = result.some(function (existingItem) {
return key.every(function (k) {
return item[k] === existingItem[k];
});
});
if (!repeat) {
result.push(item);
}
}
return result;
}
var arr = [{
"createUser": "97ef586f0c0d11eabf4f1866dae8b5e4",
"createTime": "2020-11-27 09:18:36",
"updateUser": "97ef586f0c0d11eabf4f1866dae8b5e4",
"updateTime": "2020-11-30 09:23:05",
"lbpx": 0,
"firstName": "专业管理",
"yjId": "2",
"firstDescription": "高压电缆全寿命周期管理相关标准与规范",
"secondName": "平台管理",
"ejId": "166d910d732f4b838c2d7dd246981320",
"secondDescription": "平台管理",
"thirdName": null,
"sjId": null,
"thirdDescription": null
},
{
"firstName": "专业管理",
"yjId": "2",
"firstDescription": "高压电缆全寿命周期管理相关标准与规范",
"secondName": "平台管理",
"ejId": "166d910d732f4b838c2d7dd246981320",
"secondDescription": "平台管理",
"thirdName": null,
"sjId": null,
"thirdDescription": null
},
{
"createUser": "97ef586f0c0d11eabf4f1866dae8b5e4",
"createTime": "2020-11-27 09:18:36",
"updateUser": "97ef586f0c0d11eabf4f1866dae8b5e4",
"updateTime": "2020-11-30 09:23:05",
"lbpx": 0,
"firstName": "专业管理",
"yjId": "2",
"firstDescription": "高压电缆全寿命周期管理相关标准与规范",
"secondName": "设备运维",
"ejId": "144284f6fa8c48f7907dd7614198e230",
"secondDescription": "设备运维设备运维",
"thirdName": null,
"sjId": null,
"thirdDescription": null
},
{
"firstName": "战略使命",
"yjId": "1",
"secondName": "专业使命",
"ejId": "19750b75e6654767853a301facdffe11",
"thirdName": "测试3",
"sjId": "a55c971105324db7b6dcc8b4353c1755",
"thirdDescription": "测试3测试3"
},
{
"firstName": "战略使命",
"yjId": "1",
"secondName": "战略体系",
"ejId": "1-1",
"secondDescription": "战略体系描述战略体系描述",
"thirdName": "222",
"sjId": "10418c9631c944ffa66ef4c191abe569",
"thirdDescription": "222"
},
{
"firstName": "战略使命",
"yjId": "1",
"secondName": "战略体系",
"ejId": "1-1",
"secondDescription": "战略体系描述战略体系描述",
"thirdName": "222",
"sjId": "10418c9631c944ffa66ef4c191abe569",
},
]