常用的JS数组对象API
ztj100 2024-11-25 00:39 13 浏览 0 评论
ES5及以前的Api
- ECMAScript5为数组定义了5个迭代方法,每个方法接收两个参数, 一个是每项运行的函数,一个是运行该函数的作用域对象(可选项),传入这些方法的函数会接收三个参数:数组项的值,该项在数组中的位置,数组对象本身。
// 1. Array.every()
// 全真为真
// 示例用途:全选+单选逻辑
const number = [1, 2, 3, 4, 5]
const result = number.every((item,index,array)=>{
return item > 0
})
console.log(result) // true
// 2. Array.some()
// 一真为真,与every()相反
// 示例用途:用于检测数组中是否有满足条件的项
const number = [1, 2, 3, 4, 5]
const result = number.some((item,index,array)=>{
return item > 2
})
console.log(result) // true
// 3. Array.filter()
// 数据筛选,返回 条件成立的项组成的数组
// 示例用途: 可以适用于 前端的搜索功能
const number = [1, 2, 3, 4, 5]
const result = number.filter((item,index,array)=>{
return item > 2
})
console.log(result) // [3, 4, 5]
// 4. Array.map()
// 对数组中的每一项运行改定的函数,返回每次函数调用的结果组成的数组
// 示例用途:for循环遍历操作数组中的每一项。
const number = [1, 2, 3, 4, 5]
const result = number.map((item,index,array)=>{
return item * index
})
console.log(result) // [0, 2, 6, 12, 20]
// 5. Array.forEach()
// 对数组中的每一项运行改定的函数,这个方法没有返回值
// 示例用途:for循环遍历操作数组中的每一项。
const number = [1, 2, 3, 4, 5]
const result = number.forEach((item,index,array)=>{
item * index
})
console.log(result) // [0, 2, 6, 12, 20]
// 1. Array.every()
// 全真为真
// 示例用途:全选+单选逻辑
const number = [1, 2, 3, 4, 5]
const result = number.every((item,index,array)=>{
return item > 0
})
console.log(result) // true
// 2. Array.some()
// 一真为真,与every()相反
// 示例用途:用于检测数组中是否有满足条件的项
const number = [1, 2, 3, 4, 5]
const result = number.some((item,index,array)=>{
return item > 2
})
console.log(result) // true
// 3. Array.filter()
// 数据筛选,返回 条件成立的项组成的数组
// 示例用途: 可以适用于 前端的搜索功能
const number = [1, 2, 3, 4, 5]
const result = number.filter((item,index,array)=>{
return item > 2
})
console.log(result) // [3, 4, 5]
// 4. Array.map()
// 对数组中的每一项运行改定的函数,返回每次函数调用的结果组成的数组
// 示例用途:for循环遍历操作数组中的每一项。
const number = [1, 2, 3, 4, 5]
const result = number.map((item,index,array)=>{
return item * index
})
console.log(result) // [0, 2, 6, 12, 20]
// 5. Array.forEach()
// 对数组中的每一项运行改定的函数,这个方法没有返回值
// 示例用途:for循环遍历操作数组中的每一项。
const number = [1, 2, 3, 4, 5]
const result = number.forEach((item,index,array)=>{
item * index
})
console.log(result) // [0, 2, 6, 12, 20]
- 归并方法 reduce()
- // 示例:实现数组元素累加求和 const number = [1, 2, 3, 4, 5] const result = number.reduce((prev,next,index,array) => { return prev + next }) console.log(result) // 15 // 示例:实现数组元素累加求和 const number = [1, 2, 3, 4, 5] const result = number.reduce((prev,next,index,array) => { return prev + next }) console.log(result) // 15
- 回调函数的参数:
- accumulate: 前一项,
- currentValue: 后一项,
- currentIndex: 项的索引(如果有初始值,从0开始,否则从1开始)
- arry: 数组对象
- 语法: array.reduce(callBack(accumulate,currentValue,currentIndex,array),initialValue)
- 概念: 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
- initialValue: (可选参数)作为归并基础的初始值
- reduce() 可以作为一个高阶函数,用于函数的 compose。
- 注意: reduce() 对于空数组是不会执行回调函数的。
reduceRight() 方法的功能和 reduce() 功能是一样的, 不同的是 reduceRight() 从数组的末尾向前将数组中的数组项做累加。
数组变异方法(含义:改变原数组本身,并且不需要重新赋值给原数组)
// 1. 默认排序顺序为按字母升序
const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
array1.sort()
console.log(array1) // [11, 2, 3, 4, 5, 6, 7, 8, 9]
// 2. 若要按照number形式进行排序,需要传一个函数作为参数
const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
array1.sort((a, b)=>{
// return b - a // 降序
return a - b // 升序
})
console.log(array1) // [2, 3, 4, 5, 6, 7, 8, 9, 11]
// 3. 随机排序
const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
array1.sort((a, b)=>{
return Math.random() > 0.5 ? -1 : 1
})
console.log(array1) // [4, 9, 8, 5, 6, 2, 7, 11, 3]
// 1. 默认排序顺序为按字母升序
const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
array1.sort()
console.log(array1) // [11, 2, 3, 4, 5, 6, 7, 8, 9]
// 2. 若要按照number形式进行排序,需要传一个函数作为参数
const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
array1.sort((a, b)=>{
// return b - a // 降序
return a - b // 升序
})
console.log(array1) // [2, 3, 4, 5, 6, 7, 8, 9, 11]
// 3. 随机排序
const array1 = [6, 8, 4, 3, 5, 7, 9, 11, 2]
array1.sort((a, b)=>{
return Math.random() > 0.5 ? -1 : 1
})
console.log(array1) // [4, 9, 8, 5, 6, 2, 7, 11, 3]
const array = [1, 2, 3, 4, 5]
console.log(array.join()) // 1,2,3,4,5
console.log(array.join("")) // 12345
console.log(array.join("-")) // 1-2-3-4-5
const array = [1, 2, 3, 4, 5]
console.log(array.join()) // 1,2,3,4,5
console.log(array.join("")) // 12345
console.log(array.join("-")) // 1-2-3-4-5
const array = [1, 2, 3, 4, 5]
array.reverse()
console.log(array) // [5, 4, 3, 2, 1]
const array = [1, 2, 3, 4, 5]
array.reverse()
console.log(array) // [5, 4, 3, 2, 1]
// 1. 传一个参数时,从当前位置开始,一直删除到最后,返回被删除元素所组成的数组
const array1 = [1, 2, 3, 4, 5]
const result = array1.splice(1)
console.log(result) // [2, 3, 4, 5]
console.log(array1) // [1]
// 2. 传两个参数时,从下标为 1 位置开始删除,删除 2 个 返回被删除元素所组成的数组
const array2 = [1, 2, 3, 4, 5]
const result = array2.splice(1 , 2)
console.log(result) // [2 ,3]
console.log(array2) // [1 ,4, 5]
// 3. 传三个参数时,从下标为 1 位置开始删除,删除 2 个
// ,用1替换被删除的元素 返回被删除元素所组成的数组
const array3 = [1, 2, 3, 4, 5]
const result = array3.splice(1 , 2 , 1)
console.log(result) // [2 ,3]
console.log(array3) // [1, 1, 1, 4, 5]
// 1. 传一个参数时,从当前位置开始,一直删除到最后,返回被删除元素所组成的数组
const array1 = [1, 2, 3, 4, 5]
const result = array1.splice(1)
console.log(result) // [2, 3, 4, 5]
console.log(array1) // [1]
// 2. 传两个参数时,从下标为 1 位置开始删除,删除 2 个 返回被删除元素所组成的数组
const array2 = [1, 2, 3, 4, 5]
const result = array2.splice(1 , 2)
console.log(result) // [2 ,3]
console.log(array2) // [1 ,4, 5]
// 3. 传三个参数时,从下标为 1 位置开始删除,删除 2 个
// ,用1替换被删除的元素 返回被删除元素所组成的数组
const array3 = [1, 2, 3, 4, 5]
const result = array3.splice(1 , 2 , 1)
console.log(result) // [2 ,3]
console.log(array3) // [1, 1, 1, 4, 5]
- array.splice(start,deleteCount,items)
这个方法很 :ox: :beer: 能对数组进行增删改, 返回一个新的数组 - array.reverse() 倒序操作
- array.join() 将数组按照特定的连接方式转换为字符串
- array.push() 在数组末尾添加一个或多个元素,返回数组的长度
- array.pop() 删除数组末尾的一个元素,返回被删除的元素
- array.unshift() 在数组头部添加一个或多个元素,返回数组的长度
- array.shift() 删除数组头部的一个元素,返回被删除的元素
- array.indexOf() 查找数组元素,找到返回下标,找不到返回 -1
- array.sort() 数组排序
数组非变异方法 (含义:不改变原数组本身,需要重新赋值给原数组)
const arr1 = [1, 2, 3]
const arr2 = [-1, -2, -3]
const arr3 = [11, 22, 33]
console.log(arr1.concat(arr2, arr3)) // [1, 2, 3, -1, -2, -3, 11, 22, 33]
const arr1 = [1, 2, 3]
const arr2 = [-1, -2, -3]
const arr3 = [11, 22, 33]
console.log(arr1.concat(arr2, arr3)) // [1, 2, 3, -1, -2, -3, 11, 22, 33]
const arr1 = [1, 2, 3, 4, 5, 6]
// 传两个参数,包括开始,不包括结束
const result = arr1.slice(0, 5)
console.log(result) // [1, 2, 3, 4, 5,]
// 传一个参数,slice(n)---> 从当前项到数组的最后一项截取
const result = arr1.slice(1)
console.log(result) // [2, 3, 4, 5, 6]
// 参数为负数:表示 arr1.length + 负数
// 或者理解为倒序截取
const result = arr1.slice(-5, -2)
console.log(result) // [2, 3, 4,]
// 原数组
console.log(arr1) // [1, 2, 3, 4, 5, 6]
const arr1 = [1, 2, 3, 4, 5, 6]
// 传两个参数,包括开始,不包括结束
const result = arr1.slice(0, 5)
console.log(result) // [1, 2, 3, 4, 5,]
// 传一个参数,slice(n)---> 从当前项到数组的最后一项截取
const result = arr1.slice(1)
console.log(result) // [2, 3, 4, 5, 6]
// 参数为负数:表示 arr1.length + 负数
// 或者理解为倒序截取
const result = arr1.slice(-5, -2)
console.log(result) // [2, 3, 4,]
// 原数组
console.log(arr1) // [1, 2, 3, 4, 5, 6]
- arr.slice(start, end) 数组截取
- arr.concat(arr1, arr2) 组合数组
ES6
1、Array.from()
// 概念: 将伪数组转成真正的数组,将伪数组对象或可遍历对象转换为真数组
const arrayLike = {0: 'a', 1: 'b', 2: 'c', length: 3}
const arr1 = Array.from(arrayLike)
console.log(arr1) // ['a','b','c']
// 当然也可以用Es6之前的方法
const arr2 = Array.protoype.slice.call(arrayLike)
console.log(Array.from(arr2)) // ['a','b','c']
// 概念: 将伪数组转成真正的数组,将伪数组对象或可遍历对象转换为真数组
const arrayLike = {0: 'a', 1: 'b', 2: 'c', length: 3}
const arr1 = Array.from(arrayLike)
console.log(arr1) // ['a','b','c']
// 当然也可以用Es6之前的方法
const arr2 = Array.protoype.slice.call(arrayLike)
console.log(Array.from(arr2)) // ['a','b','c']
2、Array.of()
// 将一系列数据生成一个数组
const arr1 = new Array(3)
console.log(arr1) // [empty × 3]
const arr2 = new Array(3,4)
console.log(arr2) // [3,4]
const arr3 = Array.of(3)
console.log(arr3) // [3]
// 将一系列数据生成一个数组
const arr1 = new Array(3)
console.log(arr1) // [empty × 3]
const arr2 = new Array(3,4)
console.log(arr2) // [3,4]
const arr3 = Array.of(3)
console.log(arr3) // [3]
3、Array.isArray(obj)
// 判断变量是否为数组
console.log(Array.isArray([])) // true
console.log(Array.isArray([1])) // true
console.log(Array.isArray(new Array())) // true
console.log(Array.isArray(Array.prototype) // true
// 判断变量是否为数组
console.log(Array.isArray([])) // true
console.log(Array.isArray([1])) // true
console.log(Array.isArray(new Array())) // true
console.log(Array.isArray(Array.prototype) // true
4、拓展运算:...
// 数组的展开操作
const arr1 = [1,2,3]
const arr2 = [...arr1]
arr2.push(4)
console.log(arr2) // [1,2,3,4]
// 函数定义的时候 等同于 argument的作用 ...变量名 得到真正的数组
function foo(...arr) {
console.log(...a) // 数组的展开操作 11 22 33
foo1.apply(null,a)
}
function foo1(a,b,c) {
console.log(a,b,c) // 11 22 33
}
foo(11,22,33)
// 数组的展开操作
const arr1 = [1,2,3]
const arr2 = [...arr1]
arr2.push(4)
console.log(arr2) // [1,2,3,4]
// 函数定义的时候 等同于 argument的作用 ...变量名 得到真正的数组
function foo(...arr) {
console.log(...a) // 数组的展开操作 11 22 33
foo1.apply(null,a)
}
function foo1(a,b,c) {
console.log(a,b,c) // 11 22 33
}
foo(11,22,33)
5、Array.find((item,index,arry)=>{})
// 查找,第一个符合条件的数组成员,如果没有找到,返回undefined
const arr = [1,2,3,4,5,6]
const arr1 = arr.find((item,index,arry)=>{
// return item return item > 1
})// console.log(arr1) // undefinedconsole.log(arr1) // 2
// 查找,第一个符合条件的数组成员,如果没有找到,返回undefined
const arr = [1,2,3,4,5,6]
const arr1 = arr.find((item,index,arry)=>{
// return item return item > 1
})// console.log(arr1) // undefinedconsole.log(arr1) // 2
6、Array.findIndex((item,index,arry)=>{})
// 查找,第一个符合条件的数组成员,返回其下标,没有找到则返回 -1
const arr = [1,2,3,4,5,6]
const arr1 = arr.findIndex((item,index,arry)=>{
// return item return item > 3
})// console.log(arr1) // -1console.log(arr1) // 4
// 查找,第一个符合条件的数组成员,返回其下标,没有找到则返回 -1
const arr = [1,2,3,4,5,6]
const arr1 = arr.findIndex((item,index,arry)=>{
// return item return item > 3
})// console.log(arr1) // -1console.log(arr1) // 4
7、Array.includes(searchElement[,fromIndex = 0])
// 概念:找到返回true , 找不到返回 false (可以和数组的filter方法搭配使用)
// 参数:(要找的元素[,开始查找的索引])
// 从该索引处开始查找 searchElement。 如果为负值。则按升序从
// array.length + fromIndex 的索引开始搜索。默认为0
const arr = [1,2,3,4,5,6]
const arr1 = arr.includes(3)
console.log(arr1) // true
const arr2 = arr.includes(3,-1)
// 从下标 6 - 1 = 5 开始查找3
console.log(arr2) // false
const arr3 = arr.includes(3,-10)
// 从下标 6 - 10 = -4
// 如果计算出的索引小于 0 , 则整个数组都会被搜索
console.log(arr3) // true
// 概念:找到返回true , 找不到返回 false (可以和数组的filter方法搭配使用)
// 参数:(要找的元素[,开始查找的索引])
// 从该索引处开始查找 searchElement。 如果为负值。则按升序从
// array.length + fromIndex 的索引开始搜索。默认为0
const arr = [1,2,3,4,5,6]
const arr1 = arr.includes(3)
console.log(arr1) // true
const arr2 = arr.includes(3,-1)
// 从下标 6 - 1 = 5 开始查找3
console.log(arr2) // false
const arr3 = arr.includes(3,-10)
// 从下标 6 - 10 = -4
// 如果计算出的索引小于 0 , 则整个数组都会被搜索
console.log(arr3) // true
相关推荐
- 如何将数据仓库迁移到阿里云 AnalyticDB for PostgreSQL
-
阿里云AnalyticDBforPostgreSQL(以下简称ADBPG,即原HybridDBforPostgreSQL)为基于PostgreSQL内核的MPP架构的实时数据仓库服务,可以...
- Python数据分析:探索性分析
-
写在前面如果你忘记了前面的文章,可以看看加深印象:Python数据处理...
- C++基础语法梳理:算法丨十大排序算法(二)
-
本期是C++基础语法分享的第十六节,今天给大家来梳理一下十大排序算法后五个!归并排序...
- C 语言的标准库有哪些
-
C语言的标准库并不是一个单一的实体,而是由一系列头文件(headerfiles)组成的集合。每个头文件声明了一组相关的函数、宏、类型和常量。程序员通过在代码中使用#include<...
- [深度学习] ncnn安装和调用基础教程
-
1介绍ncnn是腾讯开发的一个为手机端极致优化的高性能神经网络前向计算框架,无第三方依赖,跨平台,但是通常都需要protobuf和opencv。ncnn目前已在腾讯多款应用中使用,如QQ,Qzon...
- 用rust实现经典的冒泡排序和快速排序
-
1.假设待排序数组如下letmutarr=[5,3,8,4,2,7,1];...
- ncnn+PPYOLOv2首次结合!全网最详细代码解读来了
-
编辑:好困LRS【新智元导读】今天给大家安利一个宝藏仓库miemiedetection,该仓库集合了PPYOLO、PPYOLOv2、PPYOLOE三个算法pytorch实现三合一,其中的PPYOL...
- C++特性使用建议
-
1.引用参数使用引用替代指针且所有不变的引用参数必须加上const。在C语言中,如果函数需要修改变量的值,参数必须为指针,如...
- Qt4/5升级到Qt6吐血经验总结V202308
-
00:直观总结增加了很多轮子,同时原有模块拆分的也更细致,估计为了方便拓展个管理。把一些过度封装的东西移除了(比如同样的功能有多个函数),保证了只有一个函数执行该功能。把一些Qt5中兼容Qt4的方法废...
- 到底什么是C++11新特性,请看下文
-
C++11是一个比较大的更新,引入了很多新特性,以下是对这些特性的详细解释,帮助您快速理解C++11的内容1.自动类型推导(auto和decltype)...
- 掌握C++11这些特性,代码简洁性、安全性和性能轻松跃升!
-
C++11(又称C++0x)是C++编程语言的一次重大更新,引入了许多新特性,显著提升了代码简洁性、安全性和性能。以下是主要特性的分类介绍及示例:一、核心语言特性1.自动类型推导(auto)编译器自...
- 经典算法——凸包算法
-
凸包算法(ConvexHull)一、概念与问题描述凸包是指在平面上给定一组点,找到包含这些点的最小面积或最小周长的凸多边形。这个多边形没有任何内凹部分,即从一个多边形内的任意一点画一条线到多边形边界...
- 一起学习c++11——c++11中的新增的容器
-
c++11新增的容器1:array当时的初衷是希望提供一个在栈上分配的,定长数组,而且可以使用stl中的模板算法。array的用法如下:#include<string>#includ...
- C++ 编程中的一些最佳实践
-
1.遵循代码简洁原则尽量避免冗余代码,通过模块化设计、清晰的命名和良好的结构,让代码更易于阅读和维护...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- node卸载 (33)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- exceptionininitializererror (33)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)