判断数组对象中,同一属性,值是否相同

使用背景:筛选跳转链接,跳转链接一致,就通过跳转,不一致,就不通过

const arr = [
  {
    name: "蓝色",
    spec: "颜色",
    checked: false,
  },
  {
    name: "少年",
    spec: "适用人群",
    checked: false,
  },
  {
    name: "黑色",
    spec: "颜色",
    checked: true,
  },
  {
    name: "灰色",
    spec: "颜色",
    checked: true,
  },
  {
    name: "年轻人",
    spec: "适用人群",
    checked: false,
  },
];

判断数组中每个对象同一属性值是否相同,相同通过,反之不通过

方法一:

arr.every((e) => e.checked === false) ? console.log('通过') : console.log('不通过');  // 不通过

方法二:

arr.find((e) => e.checked === true) === undefined ? console.log('通过') : console.log('不通过');  // 不通过

方法三:

arr.findIndex((e) => e.checked === true) === -1 ? console.log('通过') : console.log('不通过');  // 不通过

方法四 — 拓展非布尔类型判断:

arr.some((e) => arr.filter((e1) => e1.spec === e.spec).length > 1) ? console.log('通过') : console.log('不通过');  // 通过

方法五 — 拓展非布尔类型判断:

arr.some((cur,idx,array)=>array.slice(idx+1).find(item=>cur.spec==item.spec)) ? console.log('通过') : console.log('不通过');  // 通过

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值