Javascript 有没有高阶函数可以遍历对象数组并返回true或false
除了foreach、map、reduce、filter、for、while和do-while之外 如果未找到属性为read:false的对象,则返回True;如果任何一个对象包含属性read:false,则返回false。 考虑下面的数组:Javascript 有没有高阶函数可以遍历对象数组并返回true或false,javascript,arrays,loops,dictionary,iterator,Javascript,Arrays,Loops,Dictionary,Iterator,除了foreach、map、reduce、filter、for、while和do-while之外 如果未找到属性为read:false的对象,则返回True;如果任何一个对象包含属性read:false,则返回false。 考虑下面的数组: let allRead = true; let notifications = [ {message: ‘Lorem’, read: true}, {message: ‘Ipsum’, read: true}, {message: ‘Dolor’, read
let allRead = true;
let notifications = [
{message: ‘Lorem’, read: true},
{message: ‘Ipsum’, read: true},
{message: ‘Dolor’, read: true},
{message: ‘Sit’, read: false},
{message: ‘Amet’, read: true}
];
必须使用上的内置高阶函数将allRead变量设置为false
通知数组。条件:a不能用于,while,do while循环b不能使用
forEach、map、reduce、filter
到目前为止,我已经使用了一些并找到了。我很确定它不是find,因为find总是返回整个对象。除了您迭代的内容之外,您不能返回其他内容
allRead = notifications.find((obj) => {
console.log("yes");
if (obj.read === false) {
console.log(obj.read);
return obj;
}
});
console.log(allRead);
另一方面,使用some取得了部分成功……但是当read:false找到时它返回true,但我想要的是,如果read:false找到,则将allRead设置为false,而不考虑其他迭代
allRead = notifications.some((not) => not.read !== true);
console.log(allRead);
我还注意到,如果我使用if-else条件或switch-case语句,并根据条件返回true、false……那么当它返回true时,它会自动中断并避免其他条件
allRead = notifications.some((not) => {
switch (not.read) {
case false:
break;
return false;
default:
return true;
}
});
console.log(allRead);
可以使用检查数组中的所有元素是否与给定条件匹配
const allRead = notifications.every(({read})=>read);
const allRead = !notifications.some(({read})=>!read);
您还可以通过简单地否定结果来检查是否没有匹配条件的元素
const allRead = notifications.every(({read})=>read);
const allRead = !notifications.some(({read})=>!read);
可以使用检查数组中的所有元素是否与给定条件匹配
const allRead = notifications.every(({read})=>read);
const allRead = !notifications.some(({read})=>!read);
您还可以通过简单地否定结果来检查是否没有匹配条件的元素
const allRead = notifications.every(({read})=>read);
const allRead = !notifications.some(({read})=>!read);
如果要检查数组列表中的所有值,在这种情况下,对象数组需要通过某些条件才能返回true(如果不是false),则可以使用array.each。 需要更多的信息或例子,如果这不是你要找的
Ref MDN:如果要检查数组列表中的所有值,在这种情况下,对象数组需要通过某些条件才能返回true(如果不是false),则可以使用array.each。 需要更多的信息或例子,如果这不是你要找的
Ref MDN:您正在查找部分或全部。我的答案有用吗?您正在查找部分或全部。我的答案有用吗?