js根据数组中某个属性进行去重

  function uniqueObjectsByProperty(arr, property) {
          return arr.reduce((acc, current) => {
            // 检查是否已存在具有相同属性值的对象
            const existingItem = acc.find(item => item[property] === current[property]);

            // 如果不存在,则将当前对象添加到结果数组中
            if (!existingItem) {
              acc.push(current);
            }

            return acc;
          }, []);
        }

    // 假设数组名为arr,每个元素是包含qwe属性的对象
    const arr = [
      { qwe: 'A', otherProp: 'value1' },
      { qwe: 'B', otherProp: 'value2' },
      { qwe: 'A', otherProp: 'value3' },
      { qwe: 'C', otherProp: 'value4' },
      { qwe: 'B', otherProp: 'value5' },
    ];

    // 传入数组和属性名进行去重
    const uniqueArr = uniqueObjectsByProperty(arr, 'qwe');
    console.log(uniqueArr);

这里演示的是根据数组中的qwe属性去重

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值