当前位置:网站首页>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
边栏推荐
- Openmldb meetup No.4 meeting minutes
- 二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
- PCL point cloud to depth image
- Hdu1234 door opener and door closer (water question)
- 洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
- V2X-Sim数据集(上海交大&纽约大学)
- [play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]
- 华为AppLinking中统一链接的创建和使用
- 如何使用IDE自动签名调试鸿蒙应用
- [applinking practical case] share in app pictures through applinking
猜你喜欢

Matlab processing of distance measurement of experimental electron microscope

【深入浅出玩转FPGA学习5-----复位设计】

二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)

最详细MySql安装教程

Hdu1228 a + B (map mapping)

【深入浅出玩转FPGA学习3-----基本语法】

How does the whole network display IP ownership?

Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)

Flick two open, realized a batch lookup join (with source code)

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
随机推荐
一招快速实现自定义快应用titlebar
Leetcode 182 Find duplicate email (2022.07.01)
Regular and common formulas
PCL extracts a subset from a point cloud
【AI应用】海康威视iVMS-4200软件安装
[AGC] build service 3 - authentication service example
Multi line display and single line display of tqdm
TIPC协议
二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
Nodejs+express+mysql simple blog building
二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)
OpenMLDB Meetup No.4 会议纪要
从攻击面视角,看信创零信任方案实践
TIPC 寻址2
String (Analog
LVM操作
mmrotate旋转目标检测框架使用记录
flink二開,實現了個 batch lookup join(附源碼)
[play with FPGA learning 5 in simple terms ----- reset design]
The difference between self and static in PHP in methods