是否可以将 .includes 用于关联数组/哈希数组?

Jac*_*Tiu 2 javascript arrays

我有以下数组需要使用 .includes 来检查它们是否是重复或没有的对象。问题是它总是返回 false 所以我不确定是否有正确的方法或 .includes 不能以这种方式使用。

var array_rooms = [{
    type: "Heritage",
    n: 1
  }, {
    type: "Hanuono",
    n: 1
  }, {
    type: "Bangon",
    n: 1
  }, {
    type: "Heritage",
    n: 1
  }]
console.log(array_rooms.includes("Heritage"));
//should return true
Run Code Online (Sandbox Code Playgroud)

31p*_*piy 6

includes非常适合搜索原语。您应该使用some来检查对象的内部属性:

var rooms = [{
  type: "Heritage",
  n: 1
}, {
  type: "Hanuono",
  n: 1
}, {
  type: "Bangon",
  n: 1
}, {
  type: "Heritage",
  n: 1
}]

console.log(rooms.some(item => item.type === "Heritage"));
Run Code Online (Sandbox Code Playgroud)