当前位置:网站首页>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 ]
边栏推荐
- 一个悄然崛起的国产软件,低调又强大!
- Myslq ten kinds of locks, an article will take you to fully analyze
- On the next generation entrance of the metauniverse -- the implementation of brain computer interface
- 一个程序员如何快速成长
- EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
-
- GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
- 【蓝桥杯Web】2022年第十三届蓝桥杯Web大学组国赛真题解析
- Summary of SQL aggregate query method for yyds dry goods inventory
- Error in installing sharp
猜你喜欢
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
面试题篇一
Arduino stepper library drive 28byj-48 stepper motor test program
牛客编程题--必刷101之字符串(高效刷题,举一反三)
EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
Servlet knowledge points
独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
Interesting! Database is also serverless!
STC 32位8051单片机开发实例教程 三 程序编译设置与下载
大厂做狼,小厂做狗?
随机推荐
Technology T3 domestic platform! Successfully equipped with "Yihui domestic real-time system sylixos"
【let var const】
Keras机器翻译实战
STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration
Battery simulation of gazebo robot
Problems encountered in installing MySQL in docker Ubuntu container
Interesting! Database is also serverless!
Use of common built-in classes of JS
上大学后明白了哪些坑人的事?
RichView RichEdit SRichViewEdit PageSize 页面设置与同步
Optimization of the problem that the request flow fails to terminate during page switching of easycvr cluster video Plaza
想得到股票开户的优惠链接,如何得知?在线开户是安全么?
Is Dao safe? Build finance encountered a malicious governance takeover and was looted!
Hls4ml/vivado HLS error reporting solution
面试题篇一
[C language] explain the usage of memset() function in detail
Example explanation: move graph explorer to jupyterlab
Win11 how to hide the taskbar? Win11 method to hide the taskbar
A quietly rising domestic software, low-key and powerful!