当前位置:网站首页>js数组常用方法
js数组常用方法
2022-07-02 07:01:00 【小猿L】
一、数组常用方法
1.Array.isArray(判断内容) 判断是否为数组,为数组返回true 不为数组返回false

2. let arr2=Array.of(参数) 定义arr2为参数组成的数组

3.let arr=Array.from(obj) 将对象装换为数组
(生成新数组)

4. 删除元素
arr.pop()删除数组最后一个元素pop

arr.shift() 删除数组第一个元素shift()

5.添加元素
arr.push(添加内容)后面添加push()

arr.unshift(1)前面添加unshift()

6反转数组arr.reverse()

7.arr.splice()删除,添加替换数组
2个参数 删除
arr.splice(起始位置,底位置);
3个参数 添加或者替换
arr.splice(开始位置,长度,替换或者添加元素);
arr.splice(0,0,100); 在开始位置添加100
arr.splice(0,1,100); 替换0~1即第一个位置元素,替换为100
arr.splice(-1,0,500) 倒数第二个位置添加500的数
arr.splice(-1,1,600)替换最后一个元素为600


8.arr.sort() 排序问题 默认升序排序,即从小到大
(1)数组元素为数字,排序
arr.sort((a,b)=>a-b);升序 从小到大
arr.sort((a,b)=>b-a);降序 从大到小

(2)数组元素为字符串,直接调用arr.sort() 方法

(3)数组对象

9.arr.copyWithin() 复制并改变数组
arr.copyWithin(index,start,end);

10.arr.fill(value,start,end)填充改变数组

11. arr.slice(start,end) 拷贝数组元素

12 arr.join() 将数组转换为字符串

13 arr.concat()连接数组

14 arr.toString()数组转换为字符串

15 arr.indexOf() 查询元素是否存在
arr.indexOf("查询元素",查询开始位置);不存在返回-1,存在返回下标位置

15 arr.lastIndexOf(value,index) 逆向查找
index为倒数第几开始查找
若有多个,返回最后一个元素位置,不然不存在返回-1

16 arr.includes(value,index) 结果返回布尔值

二、数组遍历方法
1.arr.forEach()遍历元素
arr.forEach((value,index)=>{
console.log('值为'+value);
console.log('下标为'+index);
});2.arr.some()检测数组中是否满足条件的值,拥有一个就可以
//检数组中是否有大于888的值。若有返回true,否则返回false
arr.some(value=>value>888);
3.arr.every()检测数组中是否全部满足条件
let arr=[1,2,3,4];
arr.every(value=>value>0) //返回true,全部满足条件
arr.every(value=>value>2) //返回false,只有部分满足条件4.arr.filter()过滤数组 返回新数组

5.arr.map()对每个元素进行处理,返回新数组

6.keys() 遍历键名 ,values() 遍历键值, entries() 遍历键值对
arr=['a','b','c'];
for(let i of arr.keys()){
console.log(i)//keys()遍历下标 0 1 2
}
for(let item of arr.values()){
console.log(item)//values()遍历值 a b c
}
for (let [i,item] of arr.entries()){
console.log(i,item)//entries()遍历下标以及内容
}
keys values entries
7.reduce()数组的累加器,合并成为一个值。
语法 arr.reduce((total, value, index, arr), init)
参数 total(必须):初始值,之后为上一次回调的返回值。
value(必须): 数组元素的值。
index(可选): 索引值。
arr(可选): 数组对象。
init(可选): 初始值。
返回值 :累加后的值
let arr = [1,2,3,4,5]
let sum = arr.reduce((total,value)=>total +value)
console.log(sum)//15
找出数组中有几个元素

8.find() / findIndex() 根据条件找到数组成员 [ES6]
语法arr.find(function(value, index, arr), this);
参数值同froEach一样
返回值: find()返回第一个符合条件的数组成员,若不存在返回undefined。 findIndex()返回符合条件的数组成员的索引。
let arr= [1,2,3,4,5]
let result = arr.find(item=>item>3)
console.log(result)//4 第一个值
let result1 = arr.findIndex(item=>item>3)
console.log(result1)//3 第一个值的下标
9. arr.flat() 深度遍历展开数组

数组去重排序
let arr = [1,3,2,4,8,6,[5,1,3,5,9,10,5,[1,5,3,6,7,8,11,7,66]]]
let newarr = Array.from(new Set(arr.flat(Infinity))).sort((a,b)=>a-b)
console.log(newarr)//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 66]数组打乱
var arr = [1,2,3,4,5,6,7,8,9]
for(let i =0;i<arr.length;i++){
let sum = parseInt(Math.random()*(arr.length-1))
let copyarr = arr[i];
arr[i] = arr[sum]
arr[sum] = copyarr
}
console.log(arr)都是搬运博主学习,但做笔记比较熟点
边栏推荐
- 【MySQL】连接MySQL时出现异常:Connection must be valid and open
- stm32和电机开发(上位系统)
- Shapiro Wilk normal analysis by SPSS
- [unity3d] production progress bar - make image have the functions of filled and sliced at the same time
- Pytest framework implements pre post
- 两数之和,求目标值
- flink 提交程序
- Shutter - canvas custom graph
- [visual studio] every time you open a script of unity3d, a new vs2017 will be automatically reopened
- Test -- Summary of interview questions
猜你喜欢

使用sqlcipher打开加密的sqlite方法

12. Process synchronization and semaphore

pytest框架实现前后置

Pytest learning --base

618再次霸榜的秘密何在?耐克最新财报给出答案

shell编程01_Shell基础

《MySQL 8 DBA基础教程》简介
![[visual studio] every time you open a script of unity3d, a new vs2017 will be automatically reopened](/img/c3/aec0a9fb79cf94b3575d95530aa8e8.png)
[visual studio] every time you open a script of unity3d, a new vs2017 will be automatically reopened

【JetBrain Rider】构建项目出现异常:未找到导入的项目“D:\VisualStudio2017\IDE\MSBuild\15.0\Bin\Roslyn\Microsoft.CSh

Shapiro Wilk normal analysis by SPSS
随机推荐
PCL 投影点云
AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
sqoop的表的导入
flume 190 INSTALL
UVM learning - object attribute of UVM phase
UVM - usage of common TLM port
Basic usage of mock server
axis设备的rtsp setup头中的url不能带参
Operator-1 first acquaintance with operator
SQOOP 1.4.6 INSTALL
[jetbrain rider] an exception occurred in the construction project: the imported project "d:\visualstudio2017\ide\msbuild\15.0\bin\roslyn\microsoft.csh" was not found
14.信号量的代码实现
13.信号量临界区保护
合并有序数列
Flutter——Canvas自定义曲线图
【TS】1368- 秒懂 TypeScript 泛型工具类型!
[SUCTF2018]followme
webUI自动化学习
What is the significance of the college entrance examination
Pytest learning --base