const a = zy aaa order 1 zy bbb order 1 zy aaa order 1 zy aaa order 2 ; 在数组对象a中判断:key值zy相同的数组中存在key值order相同的为true否则为false返回布尔值是或者否
可以使用reduce和some方法来解决这个问题:
const result = a.reduce((acc, currentValue) => {
const hasSameOrder = acc.some(item => item.zy === currentValue.zy && item.order === currentValue.order);
if (!hasSameOrder) {
acc.push(currentValue);
}
return acc;
}, []);
const isExist = a.length === result.length;
console.log(isExist); // true
首先,使用reduce方法将数组a进行遍历,初始值是一个空数组。
然后,在每次遍历时,使用some方法判断当前遍历到的对象与已经存在的对象中是否存在相同的zy和order值。如果存在相同的zy和order值,则将hasSameOrder的值设为true,否则为false。
最后,判断数组a和result的长度是否相等,如果相等则表示在数组对象a中,存在key值zy相同的数组中,存在key值order相同的,返回true,否则返回false
原文地址: http://www.cveoy.top/t/topic/iTnv 著作权归作者所有。请勿转载和采集!