当前位置:网站首页>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:
边栏推荐
猜你喜欢
For penetration testing methods where the output point is a timestamp (take Oracle database as an example)
MySQL compressed package installation, fool teaching
The MySQL database in Alibaba Cloud was attacked, and the data was finally recovered
阿里一面,说说你知道消息中间件的应用场景有哪些?
什么是EVM兼容链?
Fragmented NFT (Fractional NFT)
CMOS管原理,及其在推挽电路中的应用
著名网站msdn.itellyou.cn原理分析
小米手机短信定位服务激活失败
数据库 | SQL增删改查基础语法
随机推荐
MySQL高级语句(一)
Linux修改MySQL数据库密码
MySql创建数据表
Sqlite A列数据复制到B列
quick-3.6源码修改纪录
feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
cocos2d-x-3.2图片灰化效果
小白学爬虫——爬虫入门
flutter 混合开发 module 依赖
Several forms of Attribute Changer
mysql常用命令
年终总结——岁月静好~
Judgment of database in SQL injection
Pure shell implementation of text replacement
What is the difference between NFT and digital collection?
What is GameFi?
Android software security and reverse analysis reading notes
kotlin 插件更新到1.3.21
Fragmented NFT (Fractional NFT)
Common JVM interview questions and answers