类数组对象
一个类数组对象必须含有 length 属性,且元素属性名必须是数值或者可转换为数值的字符。
{
let arr = Array.from({
0: "1",
1: "faf",
2: "ad",
length: 3
});
console.log(arr); //[ '1', '2', 3 ]
}
// 没有 length 属性,则返回空数组
{
let arr1 = Array.from({
0: "1",
1: "2",
2: 3,
});
console.log(arr1); //[]
}
// 元素属性名不为数值且无法转换为数值,返回长度为 length 元素值为 undefined 的数组
{
let arr1 = Array.from({
a: 1,
b: 2,
c: 3,
length: 3
});
console.log(arr1); //[ undefined, undefined, undefined ]
}
扩展的方法
查找
find()
查找数组中符合条件的元素,若有多个符合条件的元素,则返回第一个元素。
//Es5写法
let a = Array.of(1, 2, 3, 4, 5,);
let b = a.find(function (x) {
return x > 2
})
console.log(b); //3
//Es6写法
let a1 = Array.of(1, 2, 3, 4, 5,);
let b1 = a.find( x=>x>2)
console.log(b1); //3
findIndex()
查找数组中符合条件的元素索引,若有多个符合条件的元素,则返回第一个元素索引
let a =Array.of(1,2,3,4,5,6);
[0,1,2,3,4,5]//大于4的第一个索引是4
let b = a.findIndex(x=>x>4)
console.log(b);
填充
fill()
将一定范围索引的数组元素内容填充为单个指定的值。
let arr = Array.of(1, 2, 3, 4);
// 参数1:用来填充的值
// 参数2:被填充的起始索引
// 参数3(可选):被填充的结束索引,默认为数组末尾
console.log(arr.fill(0,1,2)); // [1, 0, 3, 4]
let a = Array.of(1,2,3,4);
// 第二个参数,和第三个参数 ----指定值,表示:从哪开始填,填到那结束
let r = a.fill(0,1)
console.log(r); //[1,0,0,0]
copyWithin()
将一定范围索引的数组元素修改为此数组另一指定范围索引的元素
// 参数1:被修改的起始索引
// 参数2:被用来覆盖的数据的起始索引
// 参数3(可选):被用来覆盖的数据的结束索引,默认为数组末尾
{
let a = Array.of(1,2,3,4);
let b = a.copyWithin(0,2,3);
console.log(b); //[3,2,3,4]
let a1 = [1,2,3,4,5,6].copyWithin(2,3,5);
console.log(a1); //[1,2,4,5,5,6]
// 参数2,,,3为负数表示倒数
let b1 = [1,2,3,4,5,6].copyWithin(-2,0);
console.log(b1); //[ 1, 2, 3, 4, 1, 2 ]
}
遍历
entries()
遍历键值对
let a = [1, 2, 3];
for (let [k, v] of a.entries()) {
console.log(k + "---" + v); //0---1 1---2 2---3
}
// 不使用 for... of 循环
// next()获取数组中某个索引上的内容,获取到的内容是一个对象;
let b = a.entries();
console.log(b.next().value);
console.log(b.next().value); // [ 0, 1 ] [ 1, 2 ]
// Object.entries(a) 获取数组中某个索引上的内容
let c = Object.entries(a)[1];
console.log(c); //[ '1', 2 ]
// 数组对象
let d = [
{
id: 1,
name: "ada",
age: 15
},
{
id: 2,
name: "adja",
age: 20
}
]
let a1 = Object.entries(d)[1][1].age;
console.log(a1); //20
keys() 遍历键名。 values() 遍历键值。
// keys()遍历键名。(获取数组中所有内容的键名)
let arr = ["小红", "小明", "小花"];
for (let w of arr.keys()) {
console.log(w); // 0 1 2
}
// values()遍历值名。(获取数组中所有内容的值名)
let arr1 = ["小红", "小明", "小花"];
for (let w1 of arr1.values()) {
console.log(w1); //小红 小明 小花
}
包含
includes()
数组是否包含指定值。
注意:与 Set 和 Map 的 has 方法区分;Set 的 has 方法用于查找值;Map 的 has 方法用于查找键名。
// 参数1:包含的指定值
let a = [1, 2, 3];
if (a.includes(3)) {
console.log("true");
} else {
console.log("false");
} //true
// 同时用参数1和参数2
// 参数2:可选,搜索的起始索引,默认为0
let b = [100, 200, 100, 300, 100];
// 参数1----要找的内容;
// 参数2---从哪去找
let c = b.includes(100, 2);
console.log(c); //true
嵌套数组转一维数组
flat()
对多数组进行降维处理,flat()值默认为1
let a = [1, [2, [3, [4, [5]]]]].flat(3);
console.log(a); //[ 1, 2, 3, 4, [ 5 ] ]
//Infinity 不管嵌套多少层,无限降为,直到1维为止,注:首字母大写
let a1 = [1, [2, [3, [4, [5]]]]].flat(Infinity);
console.log(a1); // [ 1, 2, 3, 4, 5 ]
// 自动跳过空位(降为之后,会把空位上的内容underfined,自动去掉)
let a2 = [1, 2, [, 3], 4];
console.log(a2.flat()); // [ 1, 2, 3, 4 ]
flatMap()
先对数组中每个元素进行了的处理,再对数组执行 flat() 方法。
let arr= [1, 2, 3, 4, 5].flatMap(function (x) {
return [x * 20];
})
console.log(arr); //[ 20, 40, 60, 80, 100 ]
let arr1 = [2, 3, 4,].flatMap((x) => [x * 10]);
console.log(arr1); //[ 20, 30, 40 ]