首先要确保array中存在要查找的元素。其次,可以使用以下方法解决该问题:
const result = array.find(element => element.name === 'John');
if(result === undefined){ console.log('Element not found'); } else { console.log(result); }
const result = array.find(element => element.name === 'John') || 'Element not found';
console.log(result);