1. Array.of()
将括号内所有参数作为元素加入数组;若括号内为空,输出空数组
console.log(Array.of(1,"asdfg",true)); //[1, 'asdfg', true]
console.log(Array.of()); //[]
2.Array.from()
将参数中所有值作为元素形成数组
转换类数组对象
let arrayLike = {
0:'zth',
1:'19',
2:'敲代码',
length:3
}
console.log(Array.from(arrayLike)); //['zth', '19', '敲代码']
若没有length属性,则返回空数组
let arrayLike = {
0:'zth',
1:'19',
2:'敲代码',
}
console.log(Array.from(arrayLike)); //[]
若元素属性名不是按照数组那样的索引数值(或者没有从0开始一一对应),最后输出的数组中相对应的值为undefined
let arrayLike = {
0:'zth',
a:'19',
9:'敲代码',
3:'打篮球',
length:4
}
console.log(Array.from(arrayLike)); //['zth', undefined, undefined, '打篮球']
转换可迭代对象
转换map
let map = new Map();
map.set('ljp','爱敲代码');
map.set('lzy','爱内卷');
console.log(Array.from(map));
转换set
let arr = ['wq','sf','esf'];
let set = new Set(arr);
console.log(set);
console.log(Array.from(set));
转换字符串
let str = "abc";
console.log(Array.from(str)); //['a', 'b', 'c']
参数
Array.from(arrayLike[, mapFn[, thisArg]])
arrayLike
想要转换的类数组对象或可迭代对象
console.log(Array.from([1, 2, 3])); // [1, 2, 3]
mapFn
可选,map 函数,用于对每个元素进行处理,放入数组的是处理后的元素
console.log(Array.from([2,4],function(item){
return item*2
})); //[4, 8]
//也可使用箭头函数
console.log(Array.from([2, 4],(item)=>item*2)) //[4, 8]
thisArg
可选,用于指定 map 函数执行时的 this 对象
let obj = {
do:(n)=>{
return n*10;
}
}
console.log(obj.do(3)) //30
console.log(Array.from([5,6],function(item){
return this.do(item);
},obj)) //[50, 60]
3.查找
find()
查找数组中符合条件的元素,若有多个符合条件的元素,则返回第一个元素
let arr1 = Array.of(1,2,3,4);
console.log(arr1.find(item => item > 2)); //3
console.log([, 1].find(n => true)); // undefined定义未赋值
findIndex()
查找数组中符合条件的元素索引,若有多个符合条件的元素,则返回第一个元素索引
let arr2 = Array.of(1,3,5,7);
// 参数1:回调函数
// 参数2(可选):指定回调函数中的 this 值
console.log(arr2.findIndex(item => item == 3)); //1
console.log([, 1].findIndex(n => true)); //0
4.填充
fill()
将一定范围索引的数组元素内容填充为单个指定的值
let arr3 = Array.of(1, 2, 3, 4, 5);
// 参数1:用来填充的值
// 参数2:被填充的起始索引
// 参数3(可选):被填充的结束索引,默认为数组末尾(不包括自身索引)
// 相当于是[ , )
console.log(arr3.fill(0,1,3)); //[1, 0, 0, 4, 5]
copyWithin()
将一定范围索引的数组元素修改为此数组另一指定范围索引的元素
// 参数1:被修改的起始索引
// 参数2:被用来覆盖的数据的起始索引
// 参数3(可选):被用来覆盖的数据的结束索引,默认为数组末尾
console.log([1, 2, 3, 4, 5].copyWithin(0,2,4)); //[3, 4, 3, 4, 5]
// 参数1为负数表示倒数
console.log([1, 2, 3, 4, 5].copyWithin(-2, 0)); //[1, 2, 3, 1, 2]
//原数组中含有空
console.log([1, 2, ,4,5].copyWithin(0, 2, 4)); //[空白, 4, 空白, 4, 5]
5.遍历
entries()
遍历键值对
for(var [key,value] of [11,22,33].entries()){
console.log(key,value);
}
keys()
遍历键名
for(var key of [11,22,33].keys()){
console.log(key);
}
values()
遍历键值
for(var value of [11,22,33].values()){
console.log(value);
}
6.包含
includes()
数组是否包含指定值(返回boolean值)
注意:与 Set 和 Map 的 has 方法区分;Set 的 has 方法用于查找值;Map 的 has 方法用于查找键名
// 参数1:包含的指定值
// 参数2:可选,搜索的起始索引,默认为0
console.log([1, 2, 3, 4].includes(1)); //true
console.log([1, 2, 3, 4].includes(1,2)); //false
// NaN 的包含判断
console.log([1, NaN, 3].includes(NaN)); //true
7.嵌套数组转一维数组
flat()
多维数组转一维数组,从外向内解,括号内可以指定转换的层数,默认为1
还有一个值为Infinity,不管有多少层,最后转换为一维数组
console.log([1 ,[2, 3]].flat()); // [1, 2, 3]
console.log([1, [2, [3, [4, 5]]]].flat(2)); // [1, 2, 3, [4, 5]]
console.log([1, [2, [3, [4, 5]]]].flat(Infinity)); // [1, 2, 3, 4, 5]
// 自动跳过空位
console.log([1, [2, , 3]].flat()); // [1, 2, 3]
flatMap()
先对数组中每个元素进行了的处理,再对数组执行 flat() 方法
// 参数1:遍历函数,该遍历函数可接受3个参数:当前元素、当前元素索引、原数组
// 参数2:指定遍历函数中 this 的指向
console.log([1, 2, 3].flatMap(n => n * 2)); // [2, 4, 6]