当前位置:网站首页>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:
边栏推荐
- cocos2d-x-3.x 修改和纪录
- (Crypto essential dry goods) Detailed analysis of the current NFT trading markets
- Chinese garbled solution in UTF-8 environment in Powershell
- 元宇宙的前景及四大赛道
- [Elastic-Job source code analysis] - job listener
- mac10.14中安装mysqldb
- Sqlite A列数据复制到B列
- kotlin 插件更新到1.3.21
- 工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
- Artifact SSMwar exploded Error deploying artifact.See server log for details
猜你喜欢
一文速学-玩转MySQL获取时间、格式转换各类操作方法详解
Take you to understand the MySQL isolation level, what happens when two transactions operate on the same row of data at the same time?
[uiautomation] Get WeChat friend list (stored in txt)
Error: Cannot find module 'D:\Application\nodejs\node_modules\npm\bin\npm-cli.js'
GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?
对js的数组的理解
最新MySql安装教学,非常详细
mac10.14中安装mysqldb
What is the difference between NFT and digital collection?
MySql创建数据表
随机推荐
SSH自动重连脚本
How to distinguish big and small endian in C language
File operations in C language (1)
sqlmap injection tutorial common commands
cocos2d-x 实现跨平台的目录遍历
Several solutions for mysql startup error The server quit without updating PID file
quick lua加密
阿里一面,说说你知道消息中间件的应用场景有哪些?
Artifact SSMwar exploded Error deploying artifact.See server log for details
数字孪生将成为进入“元宇宙”一项重要的途径
【ubuntu20.04安装MySQL以及MySQL-workbench可视化工具】
安装Multisim出现 No software will be installed or removed解决方法
"limit" query in Oracle database
A simple bash to powershell case
Android software security and reverse analysis reading notes
(Crypto必备干货)详细分析目前NFT的几大交易市场
2021面经-拥抱变化
GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?
Getting to know regular expressions
【windows】--- SQL Server 2008 超详细安装教程