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



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

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

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"));

最新更新