i am trying to implement the optional chaining in array.find. see the below snippet code i have below three cases
- if array is empty i need to have the true // by default true
- if the array didn't find the object it should also be true // empty as true value
- if the array has the object it should take the key property value // true or false
But as per the 3 case, if the key value is false its taking the 2 one
let array = [{
id: 1,
key: false
}, {
id: 2,
key: true
}]
let key =
array && array.length ?
array.find(
(item) => item.id === 1
)?.key || "empty as true value" :
"by default true";
console.log(key)