当前位置:网站首页>js数组中修改元素的方法
js数组中修改元素的方法
2022-06-25 22:14:00 【Senora】
- 数组中添加元素的方法 .push() .unshift() .splice() .concat() …扩展运算符
- 数组中删除元素的方法 .pop() .shift() .splice()
- 数组中截取元素的方法 .slice()
一、数组中添加元素的方法
1. 使用 push 方法末尾追加元素
.push() 方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度。
var arr = [1,2,3,4,5];
arr.push(6);
console.log(arr); // [1,2,3,4,5,6]
2. 使用 unshift 方法在数组的第一项前插入一个元素
.unshift() 方法将一个或多个元素添加到数组的开头,并返回该数组的新长度。
var arr = [1,2,3,4,5];
arr.unshift(6);
console.log(arr); // [6,1,2,3,4,5]
3. 使用 splice 方法插入或删除一个元素
.splice(要修改的下标,删除个数,添加的元素) 方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。
var arr = [1,2,3,4,5];
arr.splice(5,0,6);
console.log(arr); // [1,2,3,4,5,6]
4. 使用 concat 方法合并元素
.concat()方法用于将一个数组或值合并至一个数组中,该方法不会改变原数组。
var arr = [1,2,3,4,5];
var newArr = arr.concat(6,7);
console.log(newArr); // [1,2,3,4,5,6,7]
5. 使用 … 扩展运算符合并元素
展开语法 … : 可以在数组构造时, 将数组表达式在语法层面展开。
var arr = [1,2,3,4,5];
arr = [...arr,6,7];
console.log(arr); // [1,2,3,4,5,6,7]
二、数组中删除元素的方法
1. 使用 pop 方法追加元素
.pop() 方法删除数组的末尾元素,并返回该数组的新长度。
var arr = [1,2,3,4,5];
arr.pop();
console.log(arr); // [1,2,3,4]
2. 使用 shift 方法在数组的第一项前插入一个元素
.shift() 方法删除数组的第一个元素,并返回该数组的新长度。
var arr = [1,2,3,4,5];
arr.shift();
console.log(arr); // [2,3,4,5]
三、数组中截取元素的方法
.slice(开始下标,结束下标) 方法删除数组的第一个元素,不改变原数组。
var arr = [1,2,3,4,5,6,7,8,9];
var newArr = arr.slice(2,6); // 取值范围为[2,6),即截取下标为 2,3,4,5 所对应的值
console.log(newArr); // [3,4,5,6]
边栏推荐
- How to configure SQL Server 2008 Manager_ Old bear passing by_ Sina blog
- Simulink求解器综合介绍
- yolov5 提速多GPU训练显存低的问题
- 10.4.1、数据中台
- 详解synchronize关键字
- Darkent2ncnn error
- Reading notes on how to connect the network - hubs, routers and routers (III)
- dbca静默安装及建库
- 《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
- DateTimeFormatter与LocalDateTime
猜你喜欢

EasyConnect连接后显示未分配虚拟地址

mysql 主从复制

被新冠后遗症困住15个月后,斯坦福学霸被迫缺席毕业典礼,现仍需每天卧床16小时:我本该享受20岁的人生啊...

How postman tests interfaces that require login

Recherche documentaire (3): examen des modèles de prévision de la consommation d'énergie des bâtiments fondés sur les données

dhcp复习

Recommended system design

10.2.3、Kylin_kylin的使用,维度必选

Literature research (IV): Hourly building power consumption prediction based on case-based reasoning, Ann and PCA

Explain in detail the three types of local variables, global variables and static variables
随机推荐
Use Coe_ load_ sql_ profile. SQL fixed execution plan
贴片机供料器(feeder)飞达的种类,如何工作
Bit compressor [Blue Bridge Cup training]
Smt贴片加工出现元件立碑的解决方法
Simulink求解器综合介绍
Sword finger offer 48 Longest substring without duplicate characters
10.4.1 données intermédiaires
Notes on the method of passing items from the spider file to the pipeline in the case of a scratch crawler
2021-04-28
14.1.1 promethues monitoring, four data types metrics, pushgateway
文献调研(四):基于case-based reasoning、ANN、PCA的建筑小时用电量预测
Common problems encountered when creating and publishing packages using NPM
Given the parameter n, there will be n integers 1, 2, 3,... From 1 to n, n. These n arrays have n! An arrangement that lists all columns in ascending order of size and marks them one by one. Given n a
每日刷题记录 (四)
解决线程并发安全问题
Keil compilation run error, missing error: # 5: # includecore_ cm3.h_ Old bear passing by_ Sina blog
文献调研(三):数据驱动的建筑能耗预测模型综述
被新冠后遗症困住15个月后,斯坦福学霸被迫缺席毕业典礼,现仍需每天卧床16小时:我本该享受20岁的人生啊...
no_ Expand and use_ concat
About Simple Data Visualization