简体   繁体   English

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

[英]Is it possible to use .includes for an associative array/array of hashes?

I have a following array that needs to use .includes to check if their is an object that has duplicate or none. 我有一个以下数组,需要使用.includes来检查它们是否是具有重复项或没有重复的对象。 The problem is it always returns false so I'm not sure if there is a correct way or .includes can't be used this way. 问题是它总是返回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 is well-suited to search for primitives. includes非常适合搜索基元。 You should use some to check for inner properties of objects: 您应该使用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")); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM