Home >Web Front-end >JS Tutorial >How to delete elements from set in javascript
Methods to delete elements: 1. Use delete() to delete the specified element from the Set object, the syntax is "setObj.delete(value);"; 2. Use clear() to delete the Set object All elements in the syntax "setObj.clear();".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Set collection overview
The Set collection is very similar to the Arry array, but the Set collection stores keys, which means that two values and data cannot exist in the Set collection. Keys whose types are all equal
Set collections cannot use subscripts to obtain values
Set collections do not have a length attribute but size
Set collections can be converted to real values through Array.from Array
javascript deletes elements from set
1. Use delete()
The delete() method can delete specified elements from a Set object.
Syntax: setObj.delete(value);
value: The element to be deleted
Return value:
# Returns true if deleted successfully, otherwise returns false.
Example
var mySet = new Set(); mySet.add("foo"); mySet.delete("bar"); // 返回 false,不包含 "bar" 这个元素 mySet.delete("foo"); // 返回 true,删除成功 mySet.has("foo"); // 返回 false,"foo" 已经成功删除
2. Use clear()
clear() method to delete all elements from the current Set object .
Syntax: setObj.clear();
Example:
var set = new Set(["sd",68,86,38,64,"qweq",58,"68",86]); set.clear(); //清空集合 console.log(set.size); //打印结果为0 说明集合已经被清空了 console.log(set); //打印结果{} 说明集合已经被清空了
[Related recommendations: javascript learning tutorial】
The above is the detailed content of How to delete elements from set in javascript. For more information, please follow other related articles on the PHP Chinese website!