我有一个以下数组,需要使用 .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"));