当前位置:网站首页>JS collection
JS collection
2022-07-25 10:45:00 【PBitW】
List of articles
aggregate

Collection encapsulation

In fact, the rookie just saw this , I thought it was a subset problem in my interview question , It turns out that it's not !
Ensemble method

Method realization
The way to implement collections here is Object, I think the video is a little too simple , Make the rookie feel I don't know the reason , Maybe it's originally very simple !
Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> aggregate </title>
</head>
<body>
<script>
function Set(){
// attribute
this.items = {
};
// Method
// add Method
Set.prototype.add = function(value){
// Determine whether the current set contains this element
if(this.has(value)){
return false;
}
// Add elements to the collection --> value At the same time as key and value
this.items[value] = value;
return true;
}
// remove Method
Set.prototype.remove = function(value){
// Determine whether the current set contains this element
if(!this.has(value)){
return false;
}
// Delete object key The way to
delete this.items[value];
return true;
}
// has Method
Set.prototype.has = function(value){
// key and value equally --> hasOwnProperty Detect whether a property is an object ** own property **
return this.items.hasOwnProperty(value);
}
// clear Method
Set.prototype.clear = function(){
this.items = {
};
}
// size Method
Set.prototype.size = function(){
// Object.keys() Method returns an array of enumerable properties of a given object , The order of the attribute names in the array is consistent with the order returned when the object is traversed in a normal loop
return Object.keys(this.items).length;
}
// values Method
Set.prototype.values = function(){
return Object.keys(this.items);
}
}
</script>
</body>
</html>
Rookie feels after adding notes , It seems much better !
Be careful :
Rookie discovery , there values Results returned by method alert The order of coming out is not quite right !
It should have gone through some sort of sorting ! The default is this sort , Even if you traverse normally, it is in this order !
MDN The same is true of the official website :
thank :Object.keys(…) The order of object properties ?
Assembly room operation

Combine

Code
// Combine
Set.prototype.union = function(otherSet){
//this: A collection of objects A otherSet: A collection of objects B
// 1. Create a new collection
let unionset = new Set();
// 2. take A All elements in the set are added to the new set
let values = this.values();
for(let i = 0;i < values.length;i++){
unionset.add(values[i]);
}
// 3 Take out B The elements in the collection --> Add to new collection (add The method has made de judgment )
values = otherSet.values();
for(let i = 0;i < values.length;i++){
unionset.add(values[i]);
}
return unionset;
}
intersection

Code
// intersection
Set.prototype.intersection = function(otherSet){
let intersectionSet = new Set();
// Judge this The value in ,otherSet Is there any , Add new ones if you have Set
let values = this.values();
for(let i = 0; i < values.length;i++){
let item = values[i];
if(otherSet.has(item)){
intersectionSet.add(item);
}
}
return intersectionSet;
}
Difference set

Code
// Difference set
Set.prototype.difference = function(otherSet){
let differenceSet = new Set();
let values = this.values();
// Judge this Whether the value in is not othersSet Inside , Add a new collection when you're not there
for(let i = 0;i < values.length;i++){
let item = values[i];
if(!otherSet.has(item)){
differenceSet.add(item);
}
}
return differenceSet;
}
A subset of

Although this idea still needs to be divided into situations , But really write code , Yes, it is Just cycle and judge That's it ! Because if the length is greater than otherSet, Then there must be some elements out otherSet Inside !
// A subset of
Set.prototype.subset = function(otherSet){
// 1. Traverse this The elements in , If this There is otherSet There is no such thing as , return false
let values = this.values();
for(let i = 0;i < values.length;i++){
let item = values[i];
if(!otherSet.has(item)){
return false;
}
}
return true;
}
Dictionaries
There is no code in the video here , The reason is that if you use object encapsulation, it is much like a collection , The most commonly used is to use hash table encapsulation , But I didn't learn hash table , Now let's understand , Later, I will learn the hash table to realize !

See more directly codewhy The teacher's simple book :https://www.jianshu.com/p/c53460c9c8e4
边栏推荐
- 微信小程序WxPrase中包含文件无法点击解决
- 8. SHELL file processing Three Musketeers sed
- For cycle: daffodil case
- Bug elements
- Pytorch tensor list is converted to tensor list of tensor to tensor using torch.stack()
- 思路再次完美验证!加息临近,趋势明了,好好把握这波行情!
- HCIP实验(04)
- UE4 quickly find the reason for packaging failure
- The practice of asynchronous servlet in image service
- Attention is all you need paper intensive reading notes transformer
猜你喜欢
![[Blue Bridge Cup training 100 questions] scratch Taiji diagram Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question No. 22](/img/d5/56173050f62d5b6fa336ff8d257fca.png)
[Blue Bridge Cup training 100 questions] scratch Taiji diagram Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question No. 22

2021 Niuke written examination summary 02

1. Shell programming specifications and variables
Notes on building dompteur container

Vs Code connects to the remote jupyter server

HCIP实验(01)

3. Like you, DNS domain name resolution service!!!

After switching the shell command line terminal (bash/zsh), CONDA cannot be used: command not found

Mysql5.7主从数据库部署(离线部署)

推荐系统-协同过滤在Spark中的实现
随机推荐
js 集合
C class library generation, use class library objects to data bind DataGridView
FRP reverse proxy deployment
HCIP实验(04)
2021 去哪儿网笔试总结
9.shell文本处理三剑客之awk
Idea overall font size modification
Fastdfs离线部署(图文)
Using px2rem does not take effect
The most comprehensive UE4 file operation in history, including opening, reading, writing, adding, deleting, modifying and checking
电磁场与电磁波实验一 熟悉Matlab软件在电磁场领域的应用
Angr (IV) -- angr_ ctf
How to connect tdengine through open source database management tool dbeaver
云原生IDE:iVX免费的首个通用无代码开发平台
Angr (x) - official document (Part1)
【信息系统项目管理师】思维导图系列精华汇总
Cloud native ide: the first general codeless development platform of IVX for free
Software test notes, test case design
UE4 external open EXE file
Analysis of event channel principle in Kraken

