当前位置: 首页 > news >正文

JS数组方法汇总

Array.from

//将可迭代对象或字符串转换为数组
console.log(Array.from('1234'));   //[ '1', '2', '3', '4' ]

Array.isArray

//判断是否是数组
Array.isArray(['1'])//true

Array.concat

//用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组
let arr = ['1','2','3']
let arr2 = ['4','5','6','7']
console.log(arr.concat(arr2)) //['1','2','3','4','5','6','7']

Array.copyWithin

// 改变原数组 copyWithin(target,start,end)
let arr = ['0','1','2','3']
console.log(arr.copyWithin(2,1)) //[ '0', '1', '1', '2' ]let arr = ['0','1','2','3']
console.log(arr.copyWithin(2,10)) //[ '0', '1', '2', '3' ]

Array.entries

//返回一个新的数组迭代器对象,该对象包含数组中每个索引的键/值对
let arr = ['0','1','2','3']
let arr2 = arr.entries()
console.log(arr2.next())  //{ value: [ 0, '0' ], done: false }
console.log(arr2.next()) //{ value: [ 1, '1' ], done: false }
console.log(arr2.next()) //{ value: [ 2, '2' ], done: false }
console.log(arr2.next()) //{ value: [ 3, '3' ], done: false }
console.log(arr2.next()) //{ value: undefined, done: true }

Array.every

//测试一个数组内的所有元素是否都能通过指定函数的测试。返回布尔值
let arr = ['0','1','2','3']
let arr2 = arr.every((v)=>{return v < 4
})
console.log(arr2);  //true

Array.fill

//用一个固定值填充一个数组中从起始索引(默认为 0)到终止索引(默认为 array.length)内的全部元素。它返回修改后的数组
let arr = ['0','1','2','3']
console.log(arr.fill('4')); //[ '4', '4', '4', '4' ]let arr = ['0','1','2','3']
console.log(arr.fill('4',1)); //[ '0', '4', '4', '4' ] 从下标1开始填充let arr = ['0','1','2','3']
console.log(arr.fill('4',1,3)); //[ '0', '4', '4', '3' ] 下标1到下标3填充

Array.filter

//过滤
let arr = ['0','1','2','3']
let arr2 = arr.filter(v=>{return v!=='1'
})
console.log(arr2); //[ '0', '2', '3' ]

Array.find

let arr = ['a','b','c','d']
let arr2 = arr.find(v=>{return v == 'c'
})
console.log(arr2); //c

Array.findIndex

let arr = ['a','b','c','d']
let arr2 = arr.findIndex(v=>{return v == 'c'
})
console.log(arr2); //2

Array.findLast

let arr = ['a','b','c','d','a','b','c','d']
let arr2 = arr.findLast(v=>{return v == 'c'
})
console.log(arr2); //c

Array.findLastIndex

let arr = ['a','b','c','d','a','b','c','d']
let arr2 = arr.findLastIndex(v=>{return v == 'c'
})
console.log(arr2); //6

Array.forEach

let arr = ['a','b','c','d']
arr.forEach((v,i)=>{console.log(v,i);
})
//a 0
//b 1
//c 2
//d 3

Array.flat

//数组扁平化
let arr = ['a',['b',['c',['d',['e',['f']]]]]]
console.log(arr.flat()); //[ 'a', 'b', [ 'c', [ 'd', [Array] ] ] ]
console.log(arr.flat(2)); //[ 'a', 'b', 'c', [ 'd', [ 'e', [Array] ] ] ]
console.log(arr.flat(3)); //[ 'a', 'b', 'c', 'd', [ 'e', [ 'f' ] ] ]
console.log(arr.flat(Infinity)); //[ 'a', 'b', 'c', 'd', 'e', 'f' ]

Array.includes

  let arr = ['a','b','c','d']console.log(arr.includes('b')) //true

Array.indexOf

  let arr = ['a','b','c','d']console.log(arr.indexOf('a')) //0console.log(arr.indexOf('ab')) //-1

Array.isArray

  let arr = ['a','b','c','d']console.log(Array.isArray(arr)) //true

Array.join

  let arr = ['a','b','c','d']console.log(arr.join(',')) //a,b,c,d

Array.lastIndexOf

  let arr = ['a','b','c','a','b','c']console.log(arr.lastIndexOf('b')) //4console.log(arr.lastIndexOf('1')) //-1

Array.length

  let arr = ['a','b','c']console.log(arr.length) //3

Array.map

  let arr = ['a','b','c']let arr2 = arr.map(v=>{return v+'1'})console.log(arr2) //[ 'a1', 'b1', 'c1' ]

Array.pop

//从数组中删除最后一个元素,并返回该元素的值。此方法会更改数组的长度let arr = ['a','b','c']console.log(arr.pop()) //cconsole.log(arr); //[ 'a', 'b' ]

Array.push

//将指定的元素添加到数组的末尾,并返回新的数组长度```csharplet arr = ['a','b','c']console.log(arr.push('d')) //4console.log(arr); //[ 'a', 'b', 'c', 'd' ]

Array.reduce

//对数组中的每个元素按序执行一个提供的 reducer 函数,每一次运行 reducer 会将先前元素的计算结果作为参数传入,最后将其结果汇总为单个返回值
let arr = [1,2,3,4,5,6]
arr.reduce((a,b)=>{console.log(a,b);return a+b
})
//1 2
//3 3
//6 4
//10 5
//15 6
//21let arr = [1,2,3,4,5,6]
arr.reduce((a,b)=>{console.log(a,b);return a+b
},10)
//10 1
//11 2
//13 3
//16 4
//20 5
//25 6
//31

Array.reverse

let arr = [1,2,3,4,5,6]
console.log(arr.reverse()) //[ 6, 5, 4, 3, 2, 1 ]

Array.shift

let arr = [1,2,3,4,5,6]
console.log(arr.shift()) //1
console.log(arr); //[ 2, 3, 4, 5, 6 ]

Array.slice

let arr = [1,2,3,4,5,6]
console.log(arr.slice()) //[ 1, 2, 3, 4, 5, 6 ]
console.log(arr.slice(2)) //[ 3, 4, 5, 6 ]
console.log(arr.slice(2,4)) //[ 3, 4 ]

Array.some

let arr = [1,2,3,4,5,6]
let arr2 = arr.some(v=>{return v == 3
})
console.log(arr2); //true

Array.sort

let arr = ['a','d','e','b','c','f']
let arr2 = arr.sort()
console.log(arr2); //[ 'a', 'b', 'c', 'd', 'e', 'f' ]let arr = [1,2,6,7,8,3,4,5,9,10]
let arr2 = arr.sort((a,b)=>{return a-b
})
console.log(arr2); //[1,2,6,7,8,3,4,5,9,10]

Array.splice

let arr = [1,2,3,4,5,6,7,8,9,10]
arr.splice(3) //[ 1, 2, 3 ]
arr.splice(3,6) //[ 1, 2, 3, 10 ]
arr.splice(3,6,'a') //[1,2,3,'a',8,9,10]

Array.unshift

let arr = [1,2,3]
arr.unshift(0)
console.log(arr); //[ 0, 1, 2, 3 ]

http://www.mrgr.cn/news/80999.html

相关文章:

  • 【递归,搜索与回溯算法 综合练习】深入理解暴搜决策树:递归,搜索与回溯算法综合小专题(一)
  • 轻松上手:使用 Vercel 部署 HTML 页面教程
  • Pytorch | 利用BIM/I-FGSM针对CIFAR10上的ResNet分类器进行对抗攻击
  • MONI后台管理系统-系统三员的设计
  • 如何使用 TypeScript 和 Jest 编写高质量单元测试
  • 2012年西部数学奥林匹克试题(几何)
  • 【算法】编程拓展-C语言-期末复习
  • 代码随想录算法训练营第十一天-239.滑动窗口最大值
  • 基于pytorch的深度学习基础3——模型创建与nn.Module
  • 009 Qt_显示类控件_QLCDNumber、ProgressBar、Calendar
  • 深度学习Python基础(2)
  • 移植 OLLVM 到 LLVM18,修复控制流平坦化报错
  • EdgeX Core Service 核心服务之 Meta Data 元数据
  • 精通Redis(一)
  • SpringBoot Redis 消息队列
  • JWT,OAuth 2.0,Apigee的区别与关系
  • MySQL的详细使用教程
  • .NET重点
  • iOS + watchOS Tourism App(含源码可简单复现)
  • 【Lua热更新】上篇
  • Restaurants WebAPI(三)——Serilog/
  • BenchmarkSQL使用教程
  • 使用RTP 协议 对 H264 封包和解包
  • 使用“NodeMCU”、“红外模块”实现空调控制
  • Day12 梯度下降法的含义与公式
  • php各个版本的特性以及绕过方式