0

I have 2 functions doing the same thing but for some reason the results are different.

What am i missing? TIA

let arr = [
        {first: 'Jules', last:'Atkinson'}, 
        {first: 'Ralf', last:'Benson', isInstructor: true}, 
        {first: 'Quinten', last:'Pottinger'}, 
        {first: 'Linden', last:'Knaggs', isInstructor: true}
    ]

function getCertainKey1(arr, key) { // gives me FALSE
    return arr.every((val) => {
        key in val;
    });
}

function getCertainKey2(arr, key) {  // gives me TRUE
    return arr.every(val => key in val);
}


console.log(getCertainKey1(arr, 'first'));
console.log(getCertainKey2(arr, 'first'));

Mark
  • 3
  • 1

1 Answers1

1
return arr.every((val) => {
    key in val;
});

Since there are curly braces ({}) around the body of the function, it won't actually return any values. Either say return key in val or get rid of the curly braces.

Aplet123
  • 33,825
  • 1
  • 29
  • 55