当前位置:网站首页>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
边栏推荐
猜你喜欢
![Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)](/img/c2/bb85b681af0f78b380b1d179c7ea49.png)
Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
![[applinking practical case] share in app pictures through applinking](/img/12/5616f1fa55387b81e25e55a98022b9.png)
[applinking practical case] share in app pictures through applinking

华为应用市场应用统计数据问题大揭秘

From the perspective of attack surface, see the practice of zero trust scheme of Xinchuang

Thanos Receiver

mmrotate旋转目标检测框架使用记录

What are the software product management systems? Inventory of 12 best product management tools

Hdu1228 a + B (map mapping)

Tick Data and Resampling

From Read and save in bag file Jpg pictures and PCD point cloud
随机推荐
Creation and use of unified links in Huawei applinking
[ark UI] implementation of the startup page of harmonios ETS
每月1号开始计算当月工作日
Special topic of binary tree -- acwing 1589 Building binary search tree
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
flink二开,实现了个 batch lookup join(附源码)
JSP webshell free -- webshell free
TIPC Service and Topology Tracking4
二叉树专题--AcWing 1589. 构建二叉搜索树
[quick application] win7 system cannot run and debug projects using Huawei ide
K-d tree and octree of PCL
JVM之垃圾回收器
Overview of integrated learning
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
Resources读取2d纹理 转换为png格式
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
Uncover the secrets of Huawei application market application statistics
力扣(LeetCode)182. 查找重复的电子邮箱(2022.07.01)
TIPC介绍1