可以使用 for...in 循环遍历对象的属性,结合 Array.some() 方法查找数组中是否存在特定值。以下是代码示例:
const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' }, c: { id: 3, name: 'Charlie' } }
const arr = [2, 4, 6]
const targetValue = 'Bob'
let result = false
// 遍历对象的属性 for (const prop in obj) { // 检查属性值是否等于目标值 if (obj[prop].name === targetValue) { // 使用 Array.some() 方法查找数组中是否存在属性名称对应的值 result = arr.some(value => value === obj[prop].id) break } }
console.log(result) // true