How to delete elements from set in javascript

青灯夜游
Release: 2022-09-30 15:40:37
Original
8849 people have browsed it

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();".

How to delete elements from set in javascript

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" 已经成功删除
Copy after login

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); //打印结果{} 说明集合已经被清空了
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!