当前位置:网站首页>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:
边栏推荐
猜你喜欢

Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?

腾讯云GPU桌面服务器驱动安装

On the side of Ali, tell me what are the application scenarios of message middleware you know?

阿里云中mysql数据库被攻击了,最终数据找回来了

安装Multisim出现 No software will be installed or removed解决方法

npm WARN config global `--global`, `--local` are deprecated. Use `--location solution

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

The feign call fails, JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r

Year-end summary - the years are quiet~

Eternal blue bug reappears
随机推荐
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
UiBot has an open Microsoft Edge browser and cannot perform the installation
C language tutorial (2) - printf and data types that come with c
Linux中mysql密码修改方法(亲测可用)
kotlin 插件更新到1.3.21
this指向问题
The feign call fails, JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
场效应管 | N-mos内部结构详解
Linux modify MySQL database password
【swagger关闭】生产环境关闭swagger方法
腾讯云GPU桌面服务器驱动安装
[Elastic-Job source code analysis] - job listener
js中的this指向与原型对象
阿里一面,说说你知道消息中间件的应用场景有哪些?
数据库 | SQL查询进阶语法
NFTs: The Heart of Digital Ownership
(Crypto必备干货)详细分析目前NFT的几大交易市场
元宇宙的前景及四大赛道
Gradle sync failed: Uninitialized object exists on backward branch 142
mysql password modification method in Linux (pro-test available)
https://www.w3school.com.cn/