当前位置:网站首页>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的特殊用法,不常用
边栏推荐
- One trick to quickly realize custom application titlebar
- 二叉树专题--洛谷 P1229 遍历问题(乘法原理 已知前、后序遍历求中序遍历个数)
- flink二开,实现了个 batch lookup join(附源码)
- 【付费推广】常见问题合集,推荐榜单FAQ
- Uncover the secrets of Huawei application market application statistics
- 华为AppLinking中统一链接的创建和使用
- JVM garbage collector
- The first white paper on agile practice in Chinese enterprises was released | complete download is attached
- Shell programming 01_ Shell foundation
- TIPC Getting Started6
猜你喜欢
Common methods of JS array
如何用list组件实现tabbar标题栏
二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
V2x SIM dataset (Shanghai Jiaotong University & New York University)
OpenMLDB Meetup No.4 会议纪要
二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)
HDU1234 开门人和关门人(水题)
三.芯片啟動和時鐘系統
Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
TIPC Service and Topology Tracking4
随机推荐
华为应用市场应用统计数据问题大揭秘
洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
TIPC messaging3
HDU1228 A + B(map映射)
Hdu1234 door opener and door closer (water question)
How to use ide to automatically sign and debug Hongmeng application
OpenMLDB Meetup No.4 会议纪要
flink二开,实现了个 batch lookup join(附源码)
Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)
Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
二.Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
How to transfer event objects and user-defined parameters simultaneously in Huawei express applications
Special topic of binary tree -- acwing 3384 Binary tree traversal (known preorder traversal, while building a tree, while outputting middle order traversal)
The first white paper on agile practice in Chinese enterprises was released | complete download is attached
LabVIEW为什么浮点数会丢失精度
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
[play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]