赞
踩
const myset = new set();
- mySet.add("Apple");
- mySet.add("Banana");
- mySet.add("Orange");
console.log(mySet); // 输出:Set { 'Apple', 'Banana', 'Orange' }
console.log(mySet.size); // 输出:3
- console.log(mySet.has("Banana")); // 输出:true
- console.log(mySet.has("Grapes")); // 输出:false
- mySet.delete("Orange");
- console.log(mySet); // 输出:Set { 'Apple', 'Banana' }
- mySet.clear();
- console.log(mySet); // 输出:Set {}
- const mySet = new Set([1, 2, 3]);
-
- mySet.forEach((value) => {
- console.log(value);
- });
- const mySet = new Set([1, 2, 3, 4, 5]);
-
- // 使用扩展运算符
- const myArray = [...mySet];
-
- // 使用Array.from()
- const myArray = Array.from(mySet);
-
- console.log(myArray); // 输出:[1, 2, 3, 4, 5]
- const mySet = new Set(["Apple", "Banana", "Orange"]);
-
- const myArray = Array.from(mySet);
- const myString = myArray.join(", ");
-
- console.log(myString); // 输出:"Apple, Banana, Orange"
- const mySet = new Set([
- { id: 1, name: "Apple" },
- { id: 2, name: "Banana" },
- { id: 3, name: "Orange" },
- ]);
-
- const myArray = Array.from(mySet);
- const myObject = myArray.reduce((obj, item) => {
- obj[item.id] = item.name;
- return obj;
- }, {});
-
- console.log(myObject); // 输出:{ 1: "Apple", 2: "Banana", 3: "Orange" }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。