- ①
let arrayOfObjects = [
{ id: '1', name: 'Alice' },
{ id: '2', name: 'Bob' },
{ id: '3', name: 'Charlie' }
];
let valueToCheck = 'Bob';
let exists = arrayOfObjects.some(obj => obj.name === valueToCheck);
console.log(exists); // 输出: true
- ②
let arrayOfStrings = ['Alice', 'Bob', 'Charlie'];
let valueToCheck = 'Bob';
let exists = arrayOfStrings.includes(valueToCheck);
console.log(exists); // 输出: true