当前位置:网站首页>Understanding of js arrays
Understanding of js arrays
2022-07-31 05:58:00 【messy me】
Array (Array) is an object, which is similar in function to our ordinary objects and is also used to store some values.The difference is that ordinary objects use strings as property names, while arrays use numbers as index operation elements.
Index:
The integer starting from 0 is the index. The storage performance of arrays is better than that of ordinary objects. In development, we often use arrays to store some data.
Array objects are created by adding elements:Create array object syntax:Syntax 1: var arr = new Array();Syntax 2: var arr = []; When using typeof to check an array, it will return the object type.Elements in an array can be of any data type.The elements in the array are separated by .The difference between grammar 1 and grammar 2.var arr = new Array(10); var arr = [10];Syntax 1 creates an array arr with an array length of 10, and syntax 2 creates an array with an element of 10.Add array element:
Syntax: arr[index value]=data value; such as: arr[0]=1;arr[1]=1;
Read elements in an array syntax:
Syntax: array[index].If reading an index that does not exist, it will not report an error but return undefined.
Get the length of the array:
Syntax: array.length; such as: arr.length.
Modify the length of the array
If the modified length is greater than the original length, the extra part will be vacated.
If the modified length is less than the original length, the extra elements will be deleted.
Add an element to the array
Syntax: array[array.length]=value;
2D array
When we add elements to an array as an array, the array is called a two-dimensional array.
| Method | Description | How to use |
| concat() | Concatenates two or more arrays and returns the result. | Original array name.concat(connection array name); |
| join() | Puts all elements of the array into a string.Elements are split by the specified separator. | Array name.join("Separator"); |
| pop() | Remove and return the last element of the array. | ArrayName.pop(); |
| push() | Adds one or more elements to the end of the array and returns the new length. | Array name.push("Add element"); |
| reverse() | Reverses the order of elements in the array. | ArrayName.reverse(); |
| shift() | Remove and return the first element of the array. | ArrayName.shift(); |
| slice() | Returns the selected element from an existing array. | s array name.slice(x,y); x is the starting position, y is the accommodation position. |
| sort() | Sorts the elements of the array. | ArrayName.sort(); This method sorts alphabetically and as a string. |
| splice() | Remove elements and add new elements to the array. | Array name.splice(x,y,"add element"); x is the position to add new elements, y is to delete several elements. |
| toString() | Converts the array to a string and returns the result. | ArrayName.toString(); This method returns an array as a comma-separated string |
| unshift() | Adds one or more elements to the beginning of the array and returns the new length. | Array name.unshift("Add element"); |
| valueOf() | Returns the original value of the array object. | ArrayName.valueOf(); The return result is the same as the original array. |
More other ways to view the array:
边栏推荐
- 场效应管 | N-mos内部结构详解
- Detailed explanation of pointers in C language
- Xiaobai learns reptiles - introduction to reptiles
- How to distinguish big and small endian in C language
- 为什么redis是单线程还那么快?
- C language tutorial (3) - if and loop
- 阿里一面,说说你知道消息中间件的应用场景有哪些?
- 【Elastic-Job源码分析】——作业监听器
- [Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
- 数字孪生将成为进入“元宇宙”一项重要的途径
猜你喜欢

js中的全局作用域与函数作用域

MySQL高级语句(一)

mysql password modification method in Linux (pro-test available)

UiBot has an open Microsoft Edge browser and cannot perform the installation

【云原生】原来2020.0.X版本开始的OpenFeign底层不再使用Ribbon了
![[JVM Loading]---Class Loading Mechanism](/img/b6/d1754cb6699d18602ca9a463571c0c.png)
[JVM Loading]---Class Loading Mechanism

数字孪生将成为进入“元宇宙”一项重要的途径

NFT:数字所有权的核心

【云原生】开源数据分析 SPL 轻松应对 T+0
![[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?](/img/40/be3a30743ee2bc6c82f82ab852f29c.png)
[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
随机推荐
Build vulhub vulnerability shooting range on kali
[JVM Loading]---Class Loading Mechanism
Hyper-V新建虚拟机注意事项
[Ubuntu20.04 installs MySQL and MySQL-workbench visualization tool]
js中的函数
sql 外键约束【表关系绑定】
What is an EVM Compatible Chain?
Chinese garbled solution in UTF-8 environment in Powershell
MySQL-如何分库分表?一看就懂
[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?
小白学爬虫——爬虫入门
The server time zone value ‘й‘ is unrecognized or represents more than one time zone
flutter 混合开发 module 依赖
Error: Cannot find module 'D:\Application\nodejs\node_modules\npm\bin\npm-cli.js'
Linux修改MySQL数据库密码
Pure shell implementation of text replacement
A simple bash to powershell case
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
Judgment of database in SQL injection
configure:error no SDL library found
https://www.w3school.com.cn/