当前位置:网站首页>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]
边栏推荐
- Network protocol: detailed explanation of redis protocol
- 解决线程并发安全问题
- Recherche documentaire (3): examen des modèles de prévision de la consommation d'énergie des bâtiments fondés sur les données
- Use js to obtain the last quarter based on the current quarter
- Smt贴片机工作流程
- DNS review
- logstash丢弃没有精准匹配到文件名的日志数据
- Darkent2ncnn error
- farsync 简易测试
- Topic36——53. Maximum subarray and
猜你喜欢
![[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)](/img/d9/935bad29005e5846dc514c966e3b0e.png)
[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)

Simulink求解器综合介绍

Reading notes on how to connect the network - hubs, routers and routers (III)

《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
![Search rotation array ii[Abstract dichotomy exercise]](/img/db/3ea01cf1ad8446a7007891ef1d8e7f.png)
Search rotation array ii[Abstract dichotomy exercise]

Literature research (III): overview of data-driven building energy consumption prediction models

正则表达式介绍及一些语法

Literature research (I): hourly energy consumption prediction of office buildings based on integrated learning and energy consumption pattern classification
![Bit compressor [Blue Bridge Cup training]](/img/d5/231d20bf4104cc2619b2a4f19b605c.png)
Bit compressor [Blue Bridge Cup training]

Building cloud computers with FRP
随机推荐
darkent2ncnn出错
详解synchronize关键字
logstash丢弃没有精准匹配到文件名的日志数据
anchor free dection简介
Network protocol: detailed explanation of redis protocol
Studio5k V28 installation and cracking_ Old bear passing by_ Sina blog
Multi-Instance Redo Apply
11.1.1、flink概述_flink概述
Redis memory elimination mechanism
Efficacy of kiwi fruit enzyme_ Old bear passing by_ Sina blog
每日刷题记录 (四)
正则表达式介绍及一些语法
ORA-01153 :激活了不兼容的介质恢复
SSH review
Installation of third-party library iGraph for social network visualization
Building cloud computers with FRP
Redis之哨兵
Idea common shortcut keys
tensorrt pb转uff问题
Common knowledge points in JS