Set.prototype.has()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Phương thức has() của các instance Set trả về một boolean cho biết liệu value được chỉ định có tồn tại trong Set này hay không.
Try it
const set = new Set([1, 2, 3, 4, 5]);
console.log(set.has(1));
// Expected output: true
console.log(set.has(5));
// Expected output: true
console.log(set.has(6));
// Expected output: false
Cú pháp
js
has(value)
Tham số
value-
Giá trị cần kiểm tra sự tồn tại trong đối tượng
Set.
Giá trị trả về
Trả về true nếu value được chỉ định tồn tại trong đối tượng Set; ngược lại trả về false.
Ví dụ
>Sử dụng has()
js
const mySet = new Set();
mySet.add("foo");
console.log(mySet.has("foo")); // true
console.log(mySet.has("bar")); // false
const set = new Set();
const obj = { key1: 1 };
set.add(obj);
console.log(set.has(obj)); // true
console.log(set.has({ key1: 1 })); // false, because they are different object references
console.log(set.add({ key1: 1 })); // now set contains 2 entries
Đặc tả kỹ thuật
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-set.prototype.has> |