当前位置:网站首页>What else do you not know about new set()
What else do you not know about new set()
2022-07-01 20:04:00 【Fx_ cap】
1、Set characteristic :
Set
Object is a set of values , You can iterate its elements in the order you insert them- Set The elements in will only once , namely Set The elements in are unique , So we need to judge whether the two values are equal
- Set in +0 and -0 Is a different value , have access to Object.is(+0,-0) Make a judgment and return to false ,+0 Strictly equal to -0
- Set Object allows you to store unique values of any type , Whether it's the original value or the object reference
Set in NaN
andundefined
Can be stored in Set in , however It's worth noting yes , Even though NaN !== NaN, however NaN Between Set Is regarded as the same value (NaN To be considered the same , So only one can be saved )
const set1 = new Set('abc')//{'a','b','c'}
set1.add('abc')//{'a','b','c','abc'}
const set2 = new Set('aaa')//{'a'}
set2.add(undefined)//{'a',undefined}
const set3 = new Set(['abc'])//{'abc'}
set3.add(NaN)//{'abc',NaN}
2、Set Instance properties for :
size: return Set Number of values in the object
const set1 = new Set('abc')
console.log(set1.size)// 3
const set2 = new Set('aaa')
console.log(set2.size)// 1
3、Set Instance method of :
1)、add( value ): stay Set
Add an element at the end of the object , Return to the Set
object
2)、has(value): Judge Set Whether there is an element in the object , return true/false
3)、clear( ): remove Set
All elements in the object
4)、delete( value ): The removal value is value
The elements of , And return a Boolean value to indicate whether the removal is successful also has(value)
Will return after this false
6)、entries(): Returns a new iterator object , The object contains Set
The value of all elements in the insertion order in the object [value, value]
Array . In order to make this method and Map
Objects remain similar , The key and value of each value are equal
7)、forEach(( )=>{ }): In insertion order , by Set Every value in the object is called once callBackFn
8)、keys( ): And values()
In the same way , Returns a new iterator object , The object contains Set
The values of all elements in the insertion order of the object
9)、values( ) : Returns a new iterator object , The object contains Set
The values of all elements in the insertion order of the object
let set = new Set();
set.add(1); // Set [ 1 ]
set.add(5); // Set [ 1, 5 ]
set.add(5); // Set [ 1, 5 ]// unchanged , Because there is already a value of 5 Members of
set.add("abc"); // [ 1, 5, "abc" ]
const obj = {a: 1, b: 2};
set.add(obj);//
set.add({a: 1, b: 2}); // obj It's pointing to different objects , So it can be added
set.has(1); // true
set.has(3); // false
set.has(5); // true
set.has(Math.sqrt(25)); //true Math.sqrt(25) look for 25 The square root of
set.has("Abc".toLowerCase()); // true Sensitive, case sensitive
set.has(obj); // true
set.size; // 5
set.delete(5); // true, from set Remove 5
set.has(5); // false, 5 Has been removed
set.size; // 4, Just removed a value
for (let item of mySet){
console.log(item) // Output... In sequence : 1, "abc", {"a": 1, "b": 2}, {"a": 1, "b": 2}
}
for (let item of mySet.keys()) {
console.log(item) // Output... In sequence : 1, "abc", {"a": 1, "b": 2}, {"a": 1, "b": 2}
}
for (let item of mySet.values()) {
console.log(item) // Output... In sequence : 1, "abc", {"a": 1, "b": 2}, {"a": 1, "b": 2}
}
//(key value equal )
for (let [key, value] of mySet.entries()) {
console.log(item) // Output... In sequence : 1, "abc", {"a": 1, "b": 2}, {"a": 1, "b": 2}
}
// Use Array.from transformation Set by Array
var arr = Array.from(set); // [1, "abc", {"a": 1, "b": 2}, {"a": 1, "b": 2}]
// Set and Array swap
set2 = new Set([1, 2, 3, 4]);
set2.size;// 4
[...set2]; // [1,2,3,4]
// use forEach iteration
set.forEach(function(value) {
console.log(value);// Output... In sequence a,b,c
});
4、 application : Implement common basic collections
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([2, 3]);
const setC = new Set([3, 4, 5, 6]);
A subset of
const isSuperset = (set1, set2) => { for (const item of set2) { if (!set1.has(item)) { return false; } } return true; }; console.log(' Determine if it's a subset ', isSuperset(setA, setB));//true
Combine
const union = (set1, set2) => { const res = new Set(set1); for (const item of set2) { if (!set1.has(item)) { res.add(item); } } return res; }; console.log(' Back to Union ', union(setA, setC));// Set [1, 2, 3, 4, 5, 6]
intersection
const intersection = (set1, set2) => { const res = new Set(); for (const item of set2) { if (set1.has(item)) { res.add(item); } } return res; }; console.log(' Return to intersection ', intersection(setA, setC));// Set [3, 4]
Different members form a new set
const symmetricDifference = (set1, set2) => { const res = new Set(set1); for (const item of set2) { if (res.has(item)) { res.delete(item); } else { res.add(item); } } return res; }; console.log( ' Different members form a new set ', symmetricDifference(setA, setC) );// Set [1, 2, 5, 6]
A Middle elimination B Members of
const difference = (set1, set2) => { const res = new Set(set1); for (const item of set2) { res.delete(item); } return res; }; console.log('A Middle elimination B Members of ', difference(setA, setC));// Set [1, 2 ]
边栏推荐
- Oracle 死锁测试
- docker ubuntu容器中安装mysql遇到的问题
- Realize pyramids through JS (asterisk pyramid, palindrome symmetric digital pyramid)
- Leetcode 1380 lucky numbers in matrix [array] the leetcode path of heroding
- JS proxy
- PowerDesigner设计Name和Comment 替换
- 独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
- Win11如何取消任务栏隐藏?Win11取消任务栏隐藏的方法
- Using win7 vulnerability to crack the system login password
- Develop those things: easycvr cluster device management page function display optimization
猜你喜欢
[Mysql]安装Mysql5.7
Arduino Stepper库驱动28BYJ-48步进电机测试程序
How to turn off the boot auto start software in win11
What if win11 can't pause the update? Win11 pause update is gray. How to solve it?
独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
STC 32-bit 8051 single chip microcomputer development example tutorial three program compilation setting and download
Myslq ten kinds of locks, an article will take you to fully analyze
Win11暂停更新点不了怎么办?Win11暂停更新是灰色的如何解决?
300题线性代数 第四讲 线性方程组
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
随机推荐
Anaconda installs the virtual environment to the specified path
docker ubuntu容器中安装mysql遇到的问题
Battery simulation of gazebo robot
今日群里分享的面试题
【蓝桥杯Web】2022年第十三届蓝桥杯Web大学组国赛真题解析
P2433 [deep foundation 1-2] primary school mathematics n in one
What did you learn about cheating after you went to college?
Problems encountered in installing MySQL in docker Ubuntu container
#yyds干货盘点#SQL聚合查询方法总结
关于元宇宙下一代入口——脑机接口的实现
牛客编程题--必刷101之字符串(高效刷题,举一反三)
Simple but modern server dashboard dashdot
【C语言】详解 memset() 函数用法
写博客文档
振弦采集模塊測量振弦傳感器的流程步驟
Unreal Engine packaging project
[AI server setup] CUDA environment
[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached
Install redis under Linux and configure the environment
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation