A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Set/add below:

Set.prototype.add() - JavaScript | MDN

Set.prototype.add()

Baseline Widely available

Set 实例的 add() 方法会在该集合中插入一个具有指定值的新元素,如果该 Set 对象中没有具有相同值的元素。

尝试一下
const set1 = new Set();

set1.add(42);
set1.add(42);
set1.add(13);

for (const item of set1) {
  console.log(item);
  // Expected output: 42
  // Expected output: 13
}
语法 参数
value

要添加到 Set 对象的元素的值。

返回值

添加了值的 Set 对象。

示例 使用 add() 方法
const mySet = new Set();

mySet.add(1);
mySet.add(5).add("some text"); // 可以链式调用

console.log(mySet);
// Set [1, 5, "some text"]
规范 浏览器兼容性 参见

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4