I have a similar Question to this user here: Find object by id in an array of JavaScript objects
I would like to find an Object by ID in an Array ob Objects. In MDN there was an example with Array.find().
My Problem is, that the Object can be null. Here is the example:
var myArray = [
{id:1, name:"bob"},
{id:2, name:"dan"},
{},
]
// grab the Array item which matchs the id "6"
var item = myArray.find(item => item.id === 6);
// print
console.log(item.name);
The third Object in the Array is null by some mistake. And so the item is null and so item.id is failing. How can I prevent this?