当前位置:网站首页>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的特殊用法,不常用
边栏推荐
- P1055 [noip2008 popularization group] ISBN number
- The URL in the RTSP setup header of the axis device cannot take a parameter
- HDU1228 A + B(map映射)
- 618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
- Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
- Open the encrypted SQLite method with sqlcipher
- Filtering of PCL
- 点云投影图片
- QT学习日记8——资源文件添加
- 最详细MySql安装教程
猜你喜欢

Easyexcel, a concise, fast and memory saving excel processing tool

Analysis of hot spots in AI technology industry

Read H264 parameters from mediarecord recording

HDU1228 A + B(map映射)

I STM32 development environment, keil5/mdk5.14 installation tutorial (with download link)

软件产品管理系统有哪些?12个最佳产品管理工具盘点

Leetcode+ 76 - 80 storm search topic

Overview of integrated learning

The most detailed MySQL installation tutorial

The URL in the RTSP setup header of the axis device cannot take a parameter
随机推荐
LabVIEW为什么浮点数会丢失精度
OpenMLDB Meetup No.4 会议纪要
如何使用IDE自动签名调试鸿蒙应用
What are the software product management systems? Inventory of 12 best product management tools
Convert yv12 to rgb565 image conversion, with YUV to RGB test
STM32单片机编程学习
QT学习日记7——QMainWindow
Luogu p5536 [xr-3] core city (greed + tree DP looking for the center of the tree)
Why does LabVIEW lose precision in floating point numbers
一招快速实现自定义快应用titlebar
[applinking practical case] share in app pictures through applinking
Creation and use of unified links in Huawei applinking
Overview of integrated learning
Leetcode 182 Find duplicate email (2022.07.01)
二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
C#中索引器
二叉树专题--洛谷 P1229 遍历问题(乘法原理 已知前、后序遍历求中序遍历个数)
实验电镜距离测量之Matlab处理
Luogu p4281 [ahoi2008] emergency gathering / gathering (tree doubling LCA)
Matlab processing of distance measurement of experimental electron microscope