JavaScript Set
JavaScript Set allows values to have unique values. You insert a value through add(), but if there is an existing value, the value is not added.
Declaration
const set01 = new Set();Inserting a value
set01.add('value01');
set01.add('value02');
set01.add('value03');
set01.add('value02');
set01.add(5);
// Actual inserted value
// Set(4) {'value01', 'value02', 'value03', 5}Calling values
- Values are only called by iteration.
console.log('output');
for (const item of set01) {
console.log(item);
// output
// value01
// value02
// value03
// 5Value existence
set01.has('value01');
// trueDeleting values
set01.delete('value02');
// Actual inserted value
// Set(4) {'value01', 'value03', 5}Initialize all values
set01.clear();Other
Only frequently used functions are introduced.
In addition, there are keys, values, difference, etc.
You can apply and use them if there is no duplication and they are unique.