Set Methods
중복 X, 순서 X
new Set() | Creates a new Set |
add() | Adds a new element to the Set |
delete() | Removes an element from a Set |
has() | Returns true if a value exists in the Set |
forEach() | Invokes a callback for each element in the Set |
values() | Returns an iterator with all the values in a Set |
size(Property) | Returns the number of elements in a Set |
new Set()
const letters = new Set(["a","b","c"]);
// Create a Set
const letters = new Set();
// Add Values to the Set
letters.add("a");
letters.add("b");
letters.add("c");
add()
<p id="demo"></p>
<script>
// Create a Set
const letters = new Set();
// Add values to the Set
letters.add("a");
letters.add("b");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
letters.add("c");
// Display set.size
document.getElementById("demo").innerHTML = letters.size;
</script>
3
forEach()
<p id="demo"></p>
<script>
// Create a Set
const letters = new Set(["a","b","c"]);
// List all Elements
let text = "";
letters.forEach (function(value) {
text += value + "<br>";
})
document.getElementById("demo").innerHTML = text;
</script>
a
b
c
values()
<p id="demo"></p>
<script>
// Create a Set
const letters = new Set(["a","b","c"]);
// List all Elements
let text = "";
for (const x of letters.values()) {
text += x + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
a
b
c
'JavaScript' 카테고리의 다른 글
JavaScript/jQuery - form, input 동적 생성 및 submit (0) | 2022.12.30 |
---|---|
javaScript - Map (0) | 2022.11.21 |
JavaScript - Iterables (0) | 2022.11.21 |
JavaScript - For In, For of (0) | 2022.11.17 |
JavaScript - Comparison (0) | 2022.11.17 |