当前位置:网站首页>What are the methods of adding elements to arrays in JS
What are the methods of adding elements to arrays in JS
2022-07-02 11:13:00 【xcbzsy】
js How to add elements to an array in
unshift: Add parameters to the beginning of the original array , And returns the length of the array
pop: Delete the last item of the original array , And returns the value of the deleted element ; Returns if the array is empty undefined
push: Add the parameter to the end of the original array , And returns the length of the array
concat: Returns a new array , It is composed of adding parameters to the original array
splice(start,deleteCount,val1,val2,…): from start Location start delete deleteCount term , And insert... From this position val1,val2,…
reverse: Reverse the array
sort(orderfunction): Sort the array by the specified parameters
slice(start,end): Returns a new array of items from the specified start subscript to the end subscript in the original array
detailed :
1、 Array creation
var arrayObj = new Array(); // Create an array
var arrayObj = new Array([size]); // Create an array and specify the length , Note that it's not the upper limit , It's length.
var arrayObj = new Array([element0[, element1[, …[, elementN]]]]); Create an array and assign
It should be noted that , Although the second way to create an array specifies the length , But in fact, in all cases, arrays are longer , That is to say, even if the specified length is 5, You can still store elements outside the specified length , Be careful : Then the length will change .
2、 Access to elements of an array
var testGetArrValue=arrayObj[1]; // Get element value of array
arrayObj[1]= “ This is the new value. ”; // Assign new values to array elements
3、 Adding array elements
arrayObj. push([item1 [item2 [… [itemN ]]]]);// Add one or more new elements to the end of the array , And returns the new length of the array
arrayObj.unshift([item1 [item2 [… [itemN ]]]]);// Add one or more new elements to the beginning of the array , Elements in an array are automatically moved back , Returns the new length of the array
arrayObj.splice(insertPos,0,[item1[, item2[, … [,itemN]]]]);// Inserts one or more new elements into the array at the specified location , Automatic backward movement of elements in insertion position , return ”“.
4、 Deletion of array elements
arrayObj.pop(); // Remove the last element and return the element value
arrayObj.shift(); // Remove the previous element and return the element value , Elements in array move forward automatically
arrayObj.splice(deletePos,deleteCount); // Delete from specified location deletePos Specified number of starts deleteCount The elements of , Array returns the removed element
5、 Array truncation and merging
arrayObj.slice(start, [end]); // Returns a part of an array as an array , Note not included end Corresponding elements , If omitted end Will be copied start All elements after
arrayObj.concat([item1[, item2[, … [,itemN]]]]); // Multiple arrays ( It could be a string , Or a mix of arrays and strings ) Join as an array , Return the new array connected
Arrangement :www.mls169.com
6、 Copy of array
arrayObj.slice(0); // Returns the copy array of the array , Notice it's a new array , Not pointing
arrayObj.concat(); // Returns the copy array of the array , Notice it's a new array , Not pointing
7、 Sorting of array elements
arrayObj.reverse(); // Reversal element ( Top to bottom 、 The last to the top ), Return array address
arrayObj.sort(); // Sort array elements , Return array address
8、 String of array elements
arrayObj.join(separator); // Return string , This string joins the values of each element of the array together , Intermediate use separator separate .
toLocaleString 、toString 、valueOf: It can be seen as join Special use of , Not commonly used
边栏推荐
- 2022-06-17
- Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
- sqlite 修改列类型
- 华为AppLinking中统一链接的创建和使用
- Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
- V2X-Sim数据集(上海交大&纽约大学)
- 二叉树专题--洛谷 P1229 遍历问题(乘法原理 已知前、后序遍历求中序遍历个数)
- Hdu1236 ranking (structure Sorting)
- Creation and use of unified links in Huawei applinking
- Implement custom drawer component in quick application
猜你喜欢
Hdu1236 ranking (structure Sorting)
Special topic of binary tree -- acwing 47 Path with a certain value in binary tree (preorder traversal)
Iii. Système de démarrage et d'horloge à puce
Special topic of binary tree -- acwing 1497 Traversal of the tree (use post and mid order traversal to build a binary tree)
How to use ide to automatically sign and debug Hongmeng application
[quick application] there are many words in the text component. How to solve the problem that the div style next to it will be stretched
2022-06-17
最详细MySql安装教程
Hdu1234 door opener and door closer (water question)
tidb-dm报警DM_sync_process_exists_with_error排查
随机推荐
Luogu p4281 [ahoi2008] emergency gathering / gathering (tree doubling LCA)
TIPC协议
The difference between self and static in PHP in methods
[play with FPGA learning 4 in simple terms ----- talk about state machine design]
[ark UI] implementation of the startup page of harmonios ETS
洛谷 P4281 [AHOI2008]紧急集合 / 聚会(树上倍增 LCA)
Verilog 和VHDL有符号数和无符号数相关运算
Nodejs+express+mysql simple blog building
Mongodb learning and sorting (condition operator, $type operator, limit() method, skip() method and sort() method)
Approximate sum count (approximate
【AI应用】海康威视iVMS-4200软件安装
二叉树专题--AcWing 1589. 构建二叉搜索树
TIPC Getting Started6
[applinking practical case] share in app pictures through applinking
Special topic of binary tree -- acwing 1589 Building binary search tree
spritejs
Importerror: impossible d'importer le nom « graph» de « graphviz»
Iii. Système de démarrage et d'horloge à puce
JSP webshell free -- the basis of JSP
ctf 记录