Js determines whether the array contains an element and writes a function. I don't know what went wrong.

var arr = [
    {
        id:1,
        name:"mincoo"
    },
    {
        id:2,
        name:"tomorrow"
    }
];
//:key
function isContainKey(arr,value,keyName){
    for(var i=0;i<arr.length;iPP){
        if(arr[i].keyName == value){
            return false; //
        }
        else{
            return true; //
        }
        console.log(arr[i].keyName); 
    }
}
isContainKey(arr,"tomorrow","name");

what I want:
if the array is detected to contain this element, return true, otherwise return false.
but the screen I wrote here is blank. What"s the reason?

Apr.01,2021

upstairs has explained why.
it's like officers and soldiers catching a fugitive, assuming that the fugitive is hiding in a resident's home. The officers and soldiers knocked on the door of a resident and asked if the fugitive was in your house. If it happens to be there, then the arrest is successful, and if it is not there, it is necessary to go on knocking on the door of a second house, whether it is right or not. You wrote a return, here to declare the arrest failed and went home.


the keyName attribute of the first object in the array is not equal to value .

function isContainKey(arr,value,keyName){
    for(var i=0;i<arr.length;iPP){
        if(arr[i][keyName] == value){
            return true; //
        }
    }
    return false;
}

secondly, why should we write the method? it is already there.

arr.some(({ name }) => name === 'tomorrow' )

four questions:


forreturnkeyreturnforkeytrueisContainKeyconsole.log

function isContainKey(arr,value,keyName){
    for(var i=0;i<arr.length;iPP){
      console.log(arr[i])
        if(arr[i].keyName == value){
            return true; //
        }
    }
    return false
}
Menu