关于json数组根据单个或多个key值去重问题

案例根据多个键值对去重(如果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",
         },
    ]
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值