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

标签 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

最佳答案

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

关于javascript - 是否可以将 .includes 用于关联数组/哈希数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50877691/

相关文章:

javascript - 在服务器上将 sqlite 与 HTML5 连接起来

javascript - 如何使我的表单正确自动缩放?

javascript - 当对象在javascript中有值时需要触发事件

javascript - 如何 POST 包含数组的数据对象?

javascript - 从对象数组中提取数据(问题稍微复杂)

IOS swift 如何在获取 Json 数据时检查空数组

Javascript 数组总是未定义

javascript - 部分 View 中的 MVC Ajax.BeginForm 在父 View 上执行函数更新父 View

Java - 在声明行中对静态数组进行排序(而不是在静态构造函数中)

javascript - 如何仅使用 JavaScript 将删除按钮添加到输入显示?