使用背景:筛选跳转链接,跳转链接一致,就通过跳转,不一致,就不通过
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('不通过'); // 通过
如果觉得《判断数组对象中 同一属性 值是否相同》对你有帮助,请点赞、收藏,并留下你的观点哦!