当前位置:网站首页>Introduction to arrays
Introduction to arrays
2022-07-22 19:11:00 【Please call me Gerald 6】
Array (Array)
An array is also an object , It functions like our normal objects , It is also used to store some values , The difference is that ordinary objects use strings as property names , Arrays use numbers as index elements
–> Indexes
from 0 The starting integer is the index
Array storage is better than ordinary objects , In development, we often use arrays to store some data
One :① Create an array object
var arr = new Array();
② Use literals to create arrays
var arr = [ ];
When creating arrays with literals , You can specify the elements in the array at the time of creation
var arr = [1,2,3,4,5];
–> The elements in the array can be any data type , It can also be an object , It can also be a function
arr = ["hello",1,true,null,undefined];
arr = [{
name:" The Monkey King "},{
name:" The sand monk "},{
name:" Pig eight quit "}];
arr = [function(){
alert(1)},function(){
alert(2)}];
arr[0]()// Call functions in the array
Two : Adding elements to an array
grammar :
Array [ Indexes ] = value ;
arr[0] = 10 ;
arr[1] = 20 ;
arr[2] = 30 ;
3、 ... and : Read the elements in the array
grammar :
Array [ Indexes ]
// If you read a nonexistent array , It does not report an error, but returns undefined
console.log(arr[0]);
Four : Gets the length in the array
have access to length Property to get the length of the array ( Number of elements )
grammar :
Array .length
console.log(arr.length);
For continuous arrays , Use length You can get the length of the array ( Number of elements )
For discontinuous arrays , Use length You can get the maximum index of the array +1, Try not to create discontinuous arrays
5、 ... and : modify length
If modified length Larger than the original length , Then the extra part will be empty
If modified length Less than the original length , Then the redundant part will be deleted
6、 ... and : Adds an element to the last position of the array
arr[arr.length] = 40;
7、 ... and : Array of 4 A way ( Commonly used )
①push()
This method can add one or more elements to the end of the array , And return the new length of the array
You can pass the element to be added as an argument to the method , In this way, these elements will be automatically added to the end of the array
This method returns the new length of the array as the return value
②pop()
This method can delete the last element of the array , And return the deleted element as the return value
③unshift()
Add one or more elements... To the beginning of the array , And returns the new array length
After inserting the element forward , The indexes of other elements will be adjusted in turn
④shift()
You can delete the first element of the array , And return the deleted element as the return value
⑤slice()
It can be used to extract the specified element from the array
This method does not change the element array , Instead, the intercepted elements are encapsulated into a new array and returned
Parameters
1. Index at the start of the interception , Include start index
2. Intercept the index of the end position , Does not include end index , The second parameter can be omitted , All elements from the beginning of the index will be intercepted
The index can also pass a negative value , From back to front
-1 The last one
-2 second to last
⑥splice()
Use splice() Will affect the original array , The specified element will be deleted from the original array , And return the deleted element as the return value
Parameters
first , Search index indicating the starting position
the second , Indicates the number of deletions
The third and beyond , You can pass some new elements , These elements are automatically inserted in front of the start position index
8、 ... and . Traversal of array
So called array traversal , Is to get all the elements in the array
边栏推荐
- Programmer interview golden code interview question 01.05. primary editing
- To achieve the effect of budget DLL, QT embeds the third-party window into the program, excel operation, database foreign keys, and determines whether the program is started
- Shell $*和[email protected]的区别
- 1.qt view source code
- 服务器运维环境安全体系(上篇)
- Domestic ngrok achieves intranet penetration
- CentOS7安装Mysql5.7解压版&Navicat连接Mysql&防火墙设置——亲测有效
- Unity: quick positioning camera
- Loss function in logistic regression
- numpy 求矩阵非零元素的均值
猜你喜欢
Flink learning notes (VII) processing function
融云首席科学家任杰:历练出人才,职场「经历>经验」
15 SQL optimizations commonly used by experts
fucking-algorithm
Flink learning notes (III) Flink installation and deployment
Programmer interview golden code interview question 01.02. determine whether it is character rearrangement
1. Access JSON in a way similar to the window path
Swagger-UI介绍及常用注解说明
Learning to Incorporate Structure Knowledge for Image Inpainting
MySQL master-slave replication
随机推荐
LeetCode 每日一题 2022/1/10-2022/1/16
【滑动窗口技巧】76. 最小覆盖子串
PTA 6-11 find the median of self-determined type element sequence (25 points)
The difference between shell $* and [email protected]
场景实践 | 如何使用融云超级群构建游戏社区
Data storage partition -- range partition, hash partition, list partition, and indispensable part of performance tuning
JVM-JVM概述
1. Closeable of qtablewidget, 2.pro/build_ pass、member,3.QString&&
高手常用的15 种 SQL 优化
程序员面试金典面试题 01.01. 判定字符是否唯一
Audio 3A processing practice makes your application more "pleasant"
Tcpdump simple usage
Leetcode: 185. all employees with the top three highest wages in the Department
To achieve the effect of budget DLL, QT embeds the third-party window into the program, excel operation, database foreign keys, and determines whether the program is started
Summary of all usage of join in SQL syntax (simple example)
LeetCode 每日一题 2021/11/29-2021/12/5
Writing word accumulation
卧式单面多轴钻孔组合机床动力滑台液压系统的设计
Bit and: the result of a number & 1
1. The solution of line feed qt5- "vs" in constants; 2. Problems and solutions of common compilation of QT and vs of the same code