0
点赞
收藏
分享

微信扫一扫

JavaScript高级程序设计 第4版----Array

修炼之士 2022-04-06 阅读 168

JavaScript高级程序设计 第4版----Array

在这里插入图片描述

文章目录

6.2 Array

6.2.1 创建数组

1、构造函数

let colors = new Array();
//长度为20的数组
let colors = new Array(20);
//包含 3 个字符串值的数组
let colors = new Array("red", "blue", "green");
//也可以省略操作符
let colors = Array(3); // 创建一个包含 3 个元素的数组
let names = Array("Greg"); // 创建一个只包含一个元素,即字符串"Greg"的数组

2、数组字面量

let colors = ["red", "blue", "green"]; // 创建一个包含 3 个元素的数组
let names = []; // 创建一个空数组
let values = [1,2,]; // 创建一个包含 2 个元素的数组

3、 ES6 新增了两个用于创建数组的静态方法:from()和 of()

3.1、Array.from()的第一个参数是一个类数组对象,即任何可迭代的结构,或者有一个 length 属性和可索引元素的结构。

// 字符串会被拆分为单字符数组
console.log(Array.from("Matt")); // ["M", "a", "t", "t"] 
// 可以使用 from()将集合和映射转换为一个新数组
const m = new Map().set(1, 2).set(3, 4); 
const s = new Set().add(1).add(2).add(3).add(4); 
console.log(Array.from(m)); // [[1, 2], [3, 4]] 
console.log(Array.from(s)); // [1, 2, 3, 4] 
// Array.from()对现有数组执行浅复制
const a1 = [1, 2, 3, 4]; 
const a2 = Array.from(a1); 
console.log(a1); // [1, 2, 3, 4] 
alert(a1 === a2); // false 
// 可以使用任何可迭代对象
const iter = { 
 *[Symbol.iterator]() { 
 yield 1; 
 yield 2; 
 yield 3; 
 yield 4; 
 } 
}; 
console.log(Array.from(iter)); // [1, 2, 3, 4]

// arguments 对象可以被轻松地转换为数组
function getArgsArray() { 
 return Array.from(arguments); 
} 
console.log(getArgsArray(1, 2, 3, 4)); // [1, 2, 3, 4] 
// from()也能转换带有必要属性的自定义对象
const arrayLikeObject = { 
 0: 1, 
 1: 2, 
 2: 3, 
 3: 4, 
 length: 4 
}; 
console.log(Array.from(arrayLikeObject)); // [1, 2, 3, 4]

Array.from()还接收第二个可选的映射函数参数。这个函数可以直接增强新数组的值,而无须像调用 Array.from().map()那样先创建一个中间数组。还可以接收第三个可选参数,用于指定映射函数中 this 的值。但这个重写的 this 值在箭头函数中不适用。

const a1 = [1, 2, 3, 4]; 
const a2 = Array.from(a1, x => x**2); 
const a3 = Array.from(a1, function(x) {return x**this.exponent}, {exponent: 2}); 
console.log(a2); // [1, 4, 9, 16] 
console.log(a3); // [1, 4, 9, 16] 
Array.of()可以把一组参数转换为数组。这个方法用于替代在 ES6之前常用的 Array.prototype. 
slice.call(arguments),一种异常笨拙的将 arguments 对象转换为数组的写法:
console.log(Array.of(1, 2, 3, 4)); // [1, 2, 3, 4] 
console.log(Array.of(undefined)); // [undefined]

6.2.2 数组空位

使用数组字面量初始化数组时,可以使用一串逗号来创建空位。值为undefined

const options = [,,,,,]; // 创建包含 5 个元素的数组
console.log(options.length); // 5 
console.log(options); // [,,,,,]
const options = [1,,,,5]; 
for (const option of options) { 
 console.log(option === undefined); 
} 
// false 
// true 
// true 
// true 
// false

const a = Array.from([,,,]); // 使用 ES6 的 Array.from()创建的包含 3 个空位的数组
for (const val of a) { 
 alert(val === undefined); 
} 
// true 
// true 
// true 
alert(Array.of(...[,,,])); // [undefined, undefined, undefined] 
for (const [index, value] of options.entries()) { 
 alert(value); 
} 
// 1 
// undefined 
// undefined 
// undefined 
// 5

ES6 之前的方法则会忽略这个空位,但具体的行为也会因方法而异:

const options = [1,,,,5]; 
// map()会跳过空位置
console.log(options.map(() => 6)); // [6, undefined, undefined, undefined, 6] 
// join()视空位置为空字符串
console.log(options.join('-')); // "1----5"

6.2.3 数组索引

要取得或设置数组的值,需要使用中括号并提供相应值的数字索引,如下所示:

let colors = ["red", "blue", "green"]; // 定义一个字符串数组
alert(colors[0]); // 显示第一项
colors[2] = "black"; // 修改第三项
colors[3] = "brown"; // 添加第四项

数组 length 属性不是只读的。通过修改 length 属性,可以从数组末尾删除或者添加元素

let colors = ["red", "blue", "green"]; // 创建一个包含 3 个字符串的数组
colors.length = 2; 
alert(colors[2]); // undefined

如果将 length 设置为大于数组元素数的值,则新添加的元素都将以undefined 填充

let colors = ["red", "blue", "green"]; // 创建一个包含 3 个字符串的数组
colors.length = 4; 
alert(colors[3]); // undefined
//向数据末尾添加元素
let colors = ["red", "blue", "green"]; // 创建一个包含 3 个字符串的数组
colors[colors.length] = "black"; // 添加一种颜色(位置 3)
colors[colors.length] = "brown"; // 再添加一种颜色(位置 4)

6.2.4 检测数组

1、instanceof

if (value instanceof Array){ 
 // 操作数组
}

2、Array.isArray()

if (Array.isArray(value)){ 
 // 操作数组
}

6.2.5 迭代器方法

keys()、values()和entries()

keys()返回数组索引的迭代器,values()返回数组元素的迭代器,而 entries()返回索引/值对

const a = ["foo", "bar", "baz", "qux"]; 
// 因为这些方法都返回迭代器,所以可以将它们的内容
// 通过 Array.from()直接转换为数组实例
const aKeys = Array.from(a.keys()); 
const aValues = Array.from(a.values()); 
const aEntries = Array.from(a.entries()); 
console.log(aKeys); // [0, 1, 2, 3] 
console.log(aValues); // ["foo", "bar", "baz", "qux"] 
console.log(aEntries); // [[0, "foo"], [1, "bar"], [2, "baz"], [3, "qux"]] 
使用 ES6 的解构可以非常容易地在循环中拆分键/值对:
const a = ["foo", "bar", "baz", "qux"]; 
for (const [idx, element] of a.entries()) { 
 alert(idx); 
 alert(element); 
} 
// 0 
// foo 
// 1 
// bar 
// 2 
// baz 
// 3 
// qux

6.2.6 复制和填充方法

1、批量复制copyWithin()

会按照指定范围浅复制数组中的部分内容,然后将它们插入到指定索引开始的位置

let ints, 
 reset = () => ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 
reset(); 
// 从 ints 中复制索引 0 开始的内容,插入到索引 5 开始的位置
// 在源索引或目标索引到达数组边界时停止
ints.copyWithin(5); 
console.log(ints); // [0, 1, 2, 3, 4, 0, 1, 2, 3, 4] 
reset(); 
// 从 ints 中复制索引 5 开始的内容,插入到索引 0 开始的位置
ints.copyWithin(0, 5); 
console.log(ints); // [5, 6, 7, 8, 9, 5, 6, 7, 8, 9]
reset(); 
// 从 ints 中复制索引 0 开始到索引 3 结束的内容
// 插入到索引 4 开始的位置
ints.copyWithin(4, 0, 3); 
alert(ints); // [0, 1, 2, 3, 0, 1, 2, 7, 8, 9] 
reset(); 
// JavaScript 引擎在插值前会完整复制范围内的值
// 因此复制期间不存在重写的风险
ints.copyWithin(2, 0, 6); 
alert(ints); // [0, 1, 0, 1, 2, 3, 4, 5, 8, 9] 
reset(); 
// 支持负索引值,与 fill()相对于数组末尾计算正向索引的过程是一样的
ints.copyWithin(-4, -7, -3); 
alert(ints); // [0, 1, 2, 3, 4, 5, 3, 4, 5, 6] 
copyWithin()静默忽略超出数组边界、零长度及方向相反的索引范围:
let ints, 
 reset = () => ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 
reset(); 
// 索引过低,忽略
ints.copyWithin(1, -15, -12); 
alert(ints); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 
reset() 
// 索引过高,忽略
ints.copyWithin(1, 12, 15); 
alert(ints); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 
reset(); 
// 索引反向,忽略
ints.copyWithin(2, 4, 2); 
alert(ints); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 
reset(); 
// 索引部分可用,复制、填充可用部分
ints.copyWithin(4, 7, 10) 
alert(ints); // [0, 1, 2, 3, 7, 8, 9, 7, 8, 9];

2、及填充数组fill()

使用 fill()方法可以向一个已有的数组中插入全部或部分相同的值。

开始索引用于指定开始填充的位置,它是可选的。如果不提供结束索引,则一直填充到数组末尾。负值索引从数组末尾开始计算。也可以将负索引想象成数组长度加上它得到的一个正索引:

const zeroes = [0, 0, 0, 0, 0]; 
// 用 5 填充整个数组
zeroes.fill(5); 
console.log(zeroes); // [5, 5, 5, 5, 5] 
zeroes.fill(0); // 重置
// 用 6 填充索引大于等于 3 的元素
zeroes.fill(6, 3); 
console.log(zeroes); // [0, 0, 0, 6, 6] 
zeroes.fill(0); // 重置
// 用 7 填充索引大于等于 1 且小于 3 的元素
zeroes.fill(7, 1, 3); 
console.log(zeroes); // [0, 7, 7, 0, 0]; 
zeroes.fill(0); // 重置
// 用 8 填充索引大于等于 1 且小于 4 的元素
// (-4 + zeroes.length = 1) 
// (-1 + zeroes.length = 4) 
zeroes.fill(8, -4, -1); 
console.log(zeroes); // [0, 8, 8, 8, 0];

fill()静默忽略超出数组边界、零长度及方向相反的索引范围:

const zeroes = [0, 0, 0, 0, 0]; 
// 索引过低,忽略
zeroes.fill(1, -10, -6); 
console.log(zeroes); // [0, 0, 0, 0, 0] 
// 索引过高,忽略
zeroes.fill(1, 10, 15); 
console.log(zeroes); // [0, 0, 0, 0, 0] 
// 索引反向,忽略
zeroes.fill(2, 4, 2); 
console.log(zeroes); // [0, 0, 0, 0, 0] 
// 索引部分可用,填充可用部分
zeroes.fill(4, 3, 10) 
console.log(zeroes); // [0, 0, 0, 4, 4]

6.2.7 转换方法

1、toString()

返回由数组中每个值的等效字符串拼接而成的一个逗号分隔的字符串。对数组的每个值都会调用其 toString()方法

let colors = ["red", "blue", "green"]; // 创建一个包含 3 个字符串的数组
console.log(colors.toString()); // red,blue,green

2、toLocaleString()

会得到一个逗号分隔的数组值的字符串,会调用数组每个值的 toLocaleString()方法

let person1 = { 
 toLocaleString() { 
 return "Nikolaos"; 
 }, 
 toString() { 
 return "Nicholas"; 
 } 
}; 
let person2 = { 
 toLocaleString() { 
 return "Grigorios"; 
 }, 
 toString() { 
 return "Greg"; 
 } 
}; 
let people = [person1, person2]; 
alert(people); // Nicholas,Greg  //会调用每个值的toString()方法
alert(people.toString()); // Nicholas,Greg 
alert(people.toLocaleString()); // Nikolaos,Grigorios

3、join()

可以接收一个参数,作为分隔符,不传参数或传undefined时,会默认","分割

let colors = ["red", "green", "blue"]; 
alert(colors.join(",")); // red,green,blue 
alert(colors.join("||")); // red||green||blue

6.2.8 栈方法

栈是一种后进先出的结构,也就是最近添加的项先被删除。数据项的插入(称为推入,push)和删除(称为弹出,pop)只在栈的一个地方发生,即栈顶。

1、push()

添加到数组尾部,改变原数组,返回数组的长度

2、pop()

删除数组的最后一项,改变原数组,返回被删除的项

let colors = new Array(); // 创建一个数组
let count = colors.push("red", "green"); // 推入两项
console.log(count); // 2 
count = colors.push("black"); // 再推入一项
console.log(count); // 3 
let item = colors.pop(); // 取得最后一项
console.log(item); // black 
console.log(colors.length); // 2

6.2.9 队列方法

先进先出,在末尾添加数据,在开头获取数据

1、shift()

删除第一项 返回删除的项,修改原数组

2、unshift()

在头部添加一项,返回长度 修改原数组

       
let arr =[20, 30]; 
count = arr.shift(); //删除第一项
console.log(count);  //返回删除的项 20
console.log(arr)  //[30]
let num =[40,50]; //
count = num.unshift(5); //
console.log(count); // 3 返回长度
console.log(num) // [5,40,50]

6.2.10 排序方法

1、reverse()

反向排序

let values = [1, 2, 3, 4, 5]; 
values.reverse(); 
alert(values); // 5,4,3,2,1

2、sort()

sort会按照升序重新排列数据元素,会在每一项上调用string()转型函数,即使是整数,也是通过字符串来决定顺序,

let values = [0, 1, 5, 10, 15]; 
values.sort(); 
alert(values); // 0,1,10,15,5     字符串"10"在字符串"5"的前头

以上情况在大多数情况下都不是合适的,所以sort方法接收一个比较函数,用来判断哪个值排在前面

比较函数接收两个参数,

如果第一个参数应该排在第二个参数前面,就返回负值;

如果两个参数相等,就返回 0;

如果第一个参数应该排在第二个参数后面,就返回正值。

//升序
function compare(value1, value2) { 
 if (value1 < value2) { 
 return -1; 
 } else if (value1 > value2) { 
 return 1; 
 } else { 
 return 0; 
 } 
}
let values = [0, 1, 5, 10, 15]; 
values.sort(compare); 
alert(values); // 0,1,5,10,15

//降序
function compare(value1, value2) { 
 if (value1 < value2) { 
 return 1; 
 } else if (value1 > value2) { 
 return -1; 
 } else { 
 return 0; 
 } 
} 
let values = [0, 1, 5, 10, 15]; 
values.sort(compare); 
alert(values); // 15,10,5,1,0
//简写
let values = [0, 1, 5, 10, 15]; 
values.sort((a, b) => a < b ? 1 : a > b ? -1 : 0); 
//values.sort((a, b) => a-b); 
alert(values); // 15,10,5,1,0

6.2.11 操作方法

1、concat()

let colors = ["red", "green", "blue"]; 
let colors2 = colors.concat("yellow", ["black", "brown"]); 
console.log(colors); // ["red", "green","blue"] 
console.log(colors2); // ["red", "green", "blue", "yellow", "black", "brown"]

2、slice()

用于创建一个包含原有数组中一个或多个元素的新数组。

如果只有一个参数,则 slice()会返回该索引到数组末尾的所有元素。

如果有两个参数,则 slice()返回从开始索引到结束索引对应的所有元素,其中不包含结束索引对应的元素。

不影响原数组。

let colors = ["red", "green", "blue", "yellow", "purple"]; 
console.log(colors.slice(0))  // ['red', 'green', 'blue', 'yellow', 'purple']
console.log(colors.slice(1)); // green,blue,yellow,purple 
console.log(colors.slice(1, 4)); // green,blue,yellow

3、splice()

 删除。需要给 splice()传 2 个参数:要删除的第一个元素的位置和要删除的元素数量。可以从数组中删除任意多个元素,比如 splice(0, 2)会删除前两个元素。

 插入。需要给 splice()传 3 个参数:开始位置、0(要删除的元素数量)和要插入的元素,可以在数组中指定的位置插入元素。第三个参数之后还可以传第四个、第五个参数,乃至任意多个要插入的元素。比如,splice(2, 0, “red”, “green”)会从数组位置 2 开始插入字符串"red"和"green"。

 替换。splice()在删除元素的同时可以在指定位置插入新元素,同样要传入 3 个参数:开始位置、要删除元素的数量和要插入的任意多个元素。要插入的元素数量不一定跟删除的元素数量一致。比如,splice(2, 1, “red”, “green”)会在位置 2 删除一个元素,然后从该位置开始向数组中插入"red"和"green"。

splice()返回被删除的元素,如果没有删除元素,则返回空数组

let colors = ["red", "green", "blue"]; 
let removed = colors.splice(0,1); // 删除第一项
console.log(colors); // green,blue 
console.log(removed); // red,只有一个元素的数组
removed = colors.splice(1, 0, "yellow", "orange"); // 在位置 1 插入两个元素
console.log(colors); // green,yellow,orange,blue 
console.log(removed); // 空数组
removed = colors.splice(1, 1, "red", "purple"); // 插入两个值,删除一个元素
console.log(colors); // green,red,purple,orange,blue 
console.log(removed); // yellow,只有一个元素的数组

6.2.12 搜索和位置方法

1、严格相等

1、indexOf()

返回要查找的元素在数组中的位置,如果没找到则返回-1

2、lastIndexOf()

返回要查找的元素在数组中的位置,如果没找到则返回-1

3、includes()

返回布尔值

let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; 
console.log(numbers.indexOf(4)); // 3 
console.log(numbers.lastIndexOf(4)); // 5 
console.log(numbers.includes(4)); // true 
//会进行全等(====)比较
console.log(numbers.indexOf("4")); // -1 
console.log(numbers.lastIndexOf("4")); // -1 
console.log(numbers.includes("4")); // false

console.log(numbers.indexOf(4, 4)); // 5 
console.log(numbers.lastIndexOf(4, 4)); // 3 
console.log(numbers.includes(4, 7)); // false 

let person = { name: "Nicholas" }; 
let people = [{ name: "Nicholas" }]; 
let morePeople = [person]; 
console.log(people.indexOf(person)); // -1 
console.log(morePeople.indexOf(person)); // 0 
console.log(people.includes(person)); // false 
console.log(morePeople.includes(person)); // true

2、断言函数

1、find()

返回第一个匹配的元素

2、findIndex()

返回第一个匹配元素的索引。

找到匹配项后,这两个方法都不再继续搜索。

const people = [{ 
 name: "Matt", 
 age: 27 
 }, 
 { 
 name: "Nicholas", 
 age: 29 
 } 
]; 
console.log(people.find((element, index, array) => element.age < 28)); 
// {name: "Matt", age: 27} 
console.log(people.findIndex((element, index, array) => element.age < 28));
//0

6.2.13 迭代方法

 every():对数组每一项都运行传入的函数,如果对每一项函数都返回 true,则这个方法返回 true。

 filter():对数组每一项都运行传入的函数,函数返回 true 的项会组成数组之后返回。

 forEach():对数组每一项都运行传入的函数,没有返回值。

 map():对数组每一项都运行传入的函数,返回由每次函数调用的结果构成的数组。

 some():对数组每一项都运行传入的函数,如果有一项函数返回 true,则这个方法返回 true。

let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; 
let everyResult = numbers.every((item, index, array) => item > 2); 
console.log(everyResult); // false 

let someResult = numbers.some((item, index, array) => item > 2); 
console.log(someResult); // true

let filterResult = numbers.filter((item, index, array) => item > 2); 
console.log(filterResult); // [3, 4, 5, 4, 3]

//没有返回值
let mapResult = numbers.map((item, index, array) => item * 2);
console.log(mapResult); //  [2, 4, 6, 8, 10, 8, 6, 4, 2]
numbers.forEach((item, index, array) => {
  // 执行某些操作
})

6.2.14 归并方法

接收 4 个参数:上一个归并值、当前项、当前项的索引和数组本身。这个函数返回的任何值都会作为下一次调用同一个函数的第一个参数。

1、reduce()

可以使用 reduce()函数执行累加数组中所有数值的操作,比如:
let values = [1, 2, 3, 4, 5]; 
let sum = values.reduce((prev, cur, index, array) => prev + cur); 
alert(sum); // 15

法从数组第一项开始遍历到最后一项。

2、reduceRight()

与reduce相反,从最后一项开始遍历至第一项

举报

相关推荐

0 条评论