web前端全系列 教程
1839个小节阅读:2243.5k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
数组实例的copyWithin()
方法,在当前数组内部,将指定位置的成员复制到其他位置(会覆盖原有成员),然后返回当前数组。也就是说,使用这个方法,会修改当前数组。
xxxxxxxxxx
Array.prototype.copyWithin(target, start = 0, end = this.length)
它接受三个参数
xxxxxxxxxx
[1, 2, 3, 4, 5].copyWithin(0, 3) // [4, 5, 3, 4, 5]
[1, 2, 3, 4, 5].copyWithin(0, 3, 4) // [4, 2, 3, 4, 5]
数组实例的find
方法,用于找出第一个符合条件的数组成员。它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true
的成员,然后返回该成员。如果没有符合条件的成员,则返回undefined
。
xxxxxxxxxx
[1, 5, 10, 15].find(function(value, index, arr) {
return value > 9;
}) // 10
温馨提示
第一个符合条件直接返回
数组实例的findIndex
方法的用法与find
方法非常类似,返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回-1
。
xxxxxxxxxx
[1, 5, 10, 15].findIndex(function(value, index, arr) {
return value > 9;
}) // 2
另外,这两个方法都可以发现NaN
,弥补了数组的indexOf
方法的不足。
xxxxxxxxxx
[NaN].indexOf(NaN)
// -1
[NaN].findIndex(function(value, index, arr){
if(Number.isNaN(value)){
console.log(index); // 0
}
})
fill
方法使用给定值,填充一个数组
xxxxxxxxxx
new Array(3).fill(7)
// [7, 7, 7]
['a', 'b', 'c'].fill(7)
// [7, 7, 7]
fill
方法还可以接受第二个和第三个参数,用于指定填充的起始位置和结束位置
xxxxxxxxxx
['a', 'b', 'c'].fill(7, 1, 2)
// ['a', 7, 'c']
ES6 提供三个新的方法——entries()
,keys()
和values()
——用于遍历数组。可以用for...of
循环进行遍历,唯一的区别是keys()
是对键名的遍历、values()
是对键值的遍历,entries()
是对键值对的遍历。
xxxxxxxxxx
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"
实时效果反馈
1. 下列那个数组方法可以填充数组:
A copyWithin()
B find() findIndex()
C fill
D entries(),keys() 和 values()
答案
1=>C