当前位置:网站首页>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 :
SetObject 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 NaNandundefinedCan 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)// 13、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));//trueCombine
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 ]
边栏推荐
猜你喜欢

Solve the problem of slow or failed vscode download

SQL getting started plan-1-select

Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results

STC 32位8051单片机开发实例教程 二 I/O工作模式及其配置

Procédure de mesure du capteur d'accord vibrant par le module d'acquisition d'accord vibrant

关于元宇宙下一代入口——脑机接口的实现

Simple but modern server dashboard dashdot

Oracle physical architecture

math_ Use differentiation to calculate approximate value

墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
随机推荐
开发那些事儿:EasyCVR集群设备管理页面功能展示优化
墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
Technology T3 domestic platform! Successfully equipped with "Yihui domestic real-time system sylixos"
300题线性代数 第四讲 线性方程组
数据分析师听起来很高大上?了解这几点你再决定是否转型
写博客文档
RichView TRVDocParameters 页面参数设置
Écrire un document de blog
Hls4ml entry method
简单但现代的服务器仪表板Dashdot
STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration
Solve the problem of slow or failed vscode download
人脸识别系统 —— OpenCV人脸检测
Simple but modern server dashboard dashdot
基于图的 Affinity Propagation 聚类计算公式详解和代码示例
EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
PHP获取微信小程序和小程序商店外链地址
Swiftui 4 new features complete toggle and mixed toggle multiple binding components
Is Dao safe? Build finance encountered a malicious governance takeover and was looted!