我希望find()函数true在找到'john'并停止迭代槽数时返回.或者,如果寻找名称,则返回false,比如说maria,这不在我们的任何对象中.我不明白我在这段代码中无法实现我所需要的东西?谢谢.
var array = [
{name:'paul',age:20},
{name:'john',age:30},
{name:'albert',age:40}
];
var find = function(arr){
arr.forEach(function(i){
if(i.name === 'john'){
console.log('found him');
return true;
} else {
console.log('not there');
return false;
}
});
};
find(array);
Run Code Online (Sandbox Code Playgroud)
我在这里看到了一些类似的问题,但我无法理解或理解我的问题的答案.显然我需要函数能够返回name值并同时返回true或false.