当前位置:网站首页>js中给数组添加元素的方法有哪些
js中给数组添加元素的方法有哪些
2022-07-02 07:38:00 【xcbzsy】
js中给数组添加元素的方法有哪些
unshift:将参数添加到原数组开头,并返回数组的长度
pop:删除原数组最后一项,并返回删除元素的值;如果数组为空则返回undefined
push:将参数添加到原数组末尾,并返回数组的长度
concat:返回一个新数组,是将参数添加到原数组中构成的
splice(start,deleteCount,val1,val2,…):从start位置开始删除deleteCount项,并从该位置起插入val1,val2,…
reverse:将数组反序
sort(orderfunction):按指定的参数对数组进行排序
slice(start,end):返回从原数组中指定开始下标到结束下标之间的项组成的新数组
详细:
1、数组的创建
var arrayObj = new Array(); //创建一个数组
var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长度
var arrayObj = new Array([element0[, element1[, …[, elementN]]]]); 创建一个数组并赋值
要说明的是,虽然第二种方法创建数组指定了长度,但实际上所有情况下数组都是变长的,也就是说即使指定了长度为5,仍然可以将元素存储在规定长度以外的,注意:这时长度会随之改变。
2、数组的元素的访问
var testGetArrValue=arrayObj[1]; //获取数组的元素值
arrayObj[1]= “这是新值”; //给数组元素赋予新的值
3、数组元素的添加
arrayObj. push([item1 [item2 [… [itemN ]]]]);// 将一个或多个新元素添加到数组结尾,并返回数组新长度
arrayObj.unshift([item1 [item2 [… [itemN ]]]]);// 将一个或多个新元素添加到数组开始,数组中的元素自动后移,返回数组新长度
arrayObj.splice(insertPos,0,[item1[, item2[, … [,itemN]]]]);//将一个或多个新元素插入到数组的指定位置,插入位置的元素自动后移,返回”“。
4、数组元素的删除
arrayObj.pop(); //移除最后一个元素并返回该元素值
arrayObj.shift(); //移除最前一个元素并返回该元素值,数组中元素自动前移
arrayObj.splice(deletePos,deleteCount); //删除从指定位置deletePos开始的指定数量deleteCount的元素,数组形式返回所移除的元素
5、数组的截取和合并
arrayObj.slice(start, [end]); //以数组的形式返回数组的一部分,注意不包括 end 对应的元素,如果省略 end 将复制 start 之后的所有元素
arrayObj.concat([item1[, item2[, … [,itemN]]]]); //将多个数组(也可以是字符串,或者是数组和字符串的混合)连接为一个数组,返回连接好的新的数组
整理:www.mls169.com
6、数组的拷贝
arrayObj.slice(0); //返回数组的拷贝数组,注意是一个新的数组,不是指向
arrayObj.concat(); //返回数组的拷贝数组,注意是一个新的数组,不是指向
7、数组元素的排序
arrayObj.reverse(); //反转元素(最前的排到最后、最后的排到最前),返回数组地址
arrayObj.sort(); //对数组元素排序,返回数组地址
8、数组元素的字符串化
arrayObj.join(separator); //返回字符串,这个字符串将数组的每一个元素值连接在一起,中间用 separator 隔开。
toLocaleString 、toString 、valueOf:可以看作是join的特殊用法,不常用
边栏推荐
- 2022爱分析· 国央企数字化厂商全景报告
- 二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
- Point cloud projection picture
- TIPC Getting Started6
- [quick application] win7 system cannot run and debug projects using Huawei ide
- V2X-Sim数据集(上海交大&纽约大学)
- Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)
- 全网显示 IP 归属地,是怎么实现的?
- [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
- III Chip startup and clock system
猜你喜欢

QT learning diary 7 - qmainwindow

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

Special topic of binary tree -- acwing 1497 Traversal of the tree (use post and mid order traversal to build a binary tree)

MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)

【深入浅出玩转FPGA学习4----漫谈状态机设计】

从.bag文件中读取并保存.jpg图片和.pcd点云

【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决

How to transfer event objects and user-defined parameters simultaneously in Huawei express applications

二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)

HDU1236 排名(结构体排序)
随机推荐
二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)
一招快速实现自定义快应用titlebar
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
Set the playback speed during the playback of UOB equipment
Calculate the sum of sequences
最详细MySql安装教程
TIPC协议
Special topic of binary tree -- [deep base 16. Example 7] ordinary binary tree (simplified version) (multiset seeks the precursor and subsequent sentry Art)
[play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]
QT学习日记8——资源文件添加
OpenMLDB Meetup No.4 会议纪要
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
HDU1228 A + B(map映射)
HDU1234 开门人和关门人(水题)
The most detailed MySQL installation tutorial
华为联机对战服务玩家掉线重连案例总结
Disassembling Meitu SaaS: driving the plane to change the engine
One trick to quickly realize custom application titlebar
【快应用】Win7系统使用华为IDE无法运行和调试项目