What are the collection types of JavaScript?
There are three types of collection types: 1. Map type. The Map collection stores key-value pairs. The keys cannot be repeated, but the values can be repeated. 2. Set type. The objects stored in the Set are unordered and cannot be Repeated, the objects in the collection are not sorted in a specific way; 3. List type, the objects stored in the List are ordered and repeatable, and the query speed is high.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
There are three collection types in JavaScript: set, list and map.
1. Map (key-value pairs, unique keys, non-unique values):
Map is a set of key-value pair structures, which is extremely fast search speed. Created by passing in an array of arrays. Add new elements by calling .set(key,value).
1 2 3 |
|
For example, suppose you want to find the corresponding grades based on the names of classmates. If you use Array to implement it, you need two Arrays:
1 2 |
|
Given a name, you want to find the corresponding grades. You need to first find the corresponding position in names, and then retrieve the corresponding results from scores. The longer the Array, the longer it takes.
If implemented using Map, only a "name"-"score" comparison table is needed, and the results can be searched directly based on the name. No matter how big the table is, the search speed will not slow down.
Write a Map in JavaScript as follows:
1 2 |
|
Initializing the Map requires a two-dimensional array, or directly initialize an empty Map.
Map has the following methods:
1 2 3 4 5 6 7 |
|
Since one key can only correspond to one value, if you put value into a key multiple times, the subsequent value will flush out the previous value:
1 2 3 4 |
|
1) Properties and methods
Definition: A collection of key/value pairs.
Syntax: mapObj = new Map()
Note: The keys and values in the collection can be of any type. If you add a value to the collection using an existing key, the new value replaces the old value.
Map Object Properties | Description |
---|---|
Constructor | Specify the function that creates the mapping |
Prototype — Prototype | Returns a reference to the prototype for the mapping |
Constructor | Return the number of elements in the map |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
2、Set(无序、不能重复):
Set和Map类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在Set中,没有重复的key。
Set里存放的对象是无序,不能重复的,集合中的对象不按特定的方式排序,只是简单地把对象加入集合中。
1)创建Set
Set 本身是一个构造函数,调用构造函数用来生成 Set 数据结构。
1 |
|
例
1 2 |
|
Set 函数可以接受一个数组(或类似数组的对象)作为参数,用来进行数据初始化。
1 |
|
注:如果初始化时给的值有重复的,会自动去除。集合并没有字面量声明方式。
2)Set的属性
常用的属性就一个:size 返回 Set 实例的成员总数。
1 2 |
|
3)Set的方法
Set 实例的方法分为两大类:操作方法(用于数据操作)和遍历方法(用于遍历数据)。
操作方法:
add(value) 添加数据,并返回新的 Set 结构
delete(value) 删除数据,返回一个布尔值,表示是否删除成功
has(value) 查看是否存在某个数据,返回一个布尔值
clear() 清除所有数据,没有返回值
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
遍历方法:
Set 提供了三个遍历器生成函数和一个遍历方法。
keys() 返回一个键名的遍历器
values() 返回一个键值的遍历器
entries() 返回一个键值对的遍历器
forEach() 使用回调函数遍历每个成员
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
3、List(有序、可重复) :
列表是一组有序的数据,每个列表中的数据项称为元素。
List里存放的对象是有序的,同时也是可以重复的,List关注的是索引,拥有一系列和索引相关的方法,查询速度快。因为往list集合里插入或删除数据时,会伴随着后面数据的移动,所有插入删除数据速度慢。
列表拥有描述元素位置的属性,列表有前有后(front和end)。
使用next()方法可以从当前元素移动到下一个元素,使用next() 可以从当前元素移动到下一个元素,使用prev()方法可以移动到当前元素的前一个元素,还可以使用moveTo(n)方法直接移动到指定位置
1)List的方法:
定义的属性有:
listSize : 列表的元素个数
pos: 列表的当前位置
定义的方法有:
getElement(): 返回当前位置的元素
insert(): 在现有元素后插入新元素
append(): 在列表的尾部添加新元素
remove(): 从列表中删除元素
length(): 返回列表中元素的个数
clear(): 清空列表
contains(): 判断元素是否存在列表中
移动元素:
front(): 将列表的当前位置移动到第一个元素
end(): 将列表的当前位置移动到最后一个元素
prev(): 将当前位置后移一位
next(): 将当前位置前移一位
currPos(): 返回列表的当前位置
moveTo(): 将当前位置移动到指定位置
2)List的实现
使用数组实现一个列表,并初始化属性值
1 2 3 4 5 |
|
append(element) 添加元素
给列表尾部添加新元素的方法与栈的压栈方法一样;
将记录元素个数的listSize加1,从而获取到存储元素的位置;再将元素添加到这个位置;
1 2 3 4 |
|
find(element) 查找元素
首先遍历列表,如果要查找的元素存在列表中,则返回该元素的位置,未找到则返回-1
1 2 3 4 5 6 7 8 9 10 11 |
|
remove(element) 删除元素
先调用find方法,查找元素的位置,如果存在返回true,不存在则会返回false;
如果存在,使用js数组操作方法splice删除当前元素,splice方法接收两个参数,即要删除的元素的索引和要删除的个数;
删除元素后,要将列表的长度减1;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
length() 查询列表中有多少元素
该方法返回列表中的元素,直接返回listSize的值
1 2 3 |
|
insert(element, after) 向列表中插入元素
该方法是将目标元素插入到指定元素的后面,它接收两个参数,即目标元素element和指定元素after;
实现:先使用定义的find方法找到after的位置,然后使用splice方法在该位置的后一位插入目标元素;
splice方法传入三个参数,目标值的位置,要删除的数量,要插入的值(第二个参数为0表示删除0个,不删除元素);
操作成功返回true, 未找到指定元素则返回false
1 2 3 4 5 6 7 8 9 |
|
clear() 清空列表
删除列表,初始化数据
1 2 3 4 5 6 |
|
contains(element) 判断元素是否存在列表中
与find方法类似,也可直接使用find方法查找,找到返回true,不存在返回false
1 2 3 4 5 6 7 8 |
|
迭代器:移动列表中的元素
创建手动迭代列表的一些方法,可以不用关心数据的内部存储方法,以实现对列表的遍历。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
【相关推荐:javascript视频教程、web前端】
The above is the detailed content of What are the collection types of JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
