JavaScript 中的数组

在 JavaScript 中,数组是一个对象,允许你将一组值存储在一个单一的变量名下。这与许多其他编程语言相同,但也有一些区别。

  • 数组中的值不需要是相同的数据类型。你可以将任何你想要的东西放入数组中,稍后再处理数据类型(不过也有类型化数组,其中所有值的数据类型必须相同)。
  • 当你创建一个数组时,不需要声明大小——但你可以声明。数组会自动增长。这使得数组非常方便使用,但不适合数值分析类的应用。
  • 因为数组是对象,所以它们具有可调用的方法和属性。例如,.length 属性表示数组中当前的元素数量。如果你向数组添加更多的元素,.length 的值会变大。
  • 元素的计数从 0 开始,这意味着,例如,第 5 个元素位于 [4] 位置。

提示:使用数组时,应始终使用带有非负整数的方括号表示法,例如:arr[3] = 42;。严格来说,也可以使用点表示法,但这通常会导致意外的行为,尤其是对初学者来说。

创建数组

与所有对象一样,数组也有一个构造函数。

"use strict";

const arr_1 = new Array(); // 空数组
alert(arr_1.length);
const arr_2 = new Array(0, 2, 4); // 3 个元素
alert(arr_2);

接下来,JavaScript 语法支持使用方括号来创建和操作数组。

"use strict";

const arr_1 = []; // 空数组
alert(arr_1.length);
const arr_2 = [0, 2, 4]; // 3 个元素
alert(arr_2);

你可以在声明数组时预定义数组的大小。

"use strict";

const arr_3 = new Array(50); // 50 个元素
alert(arr_3.length);

访问数组元素

数组元素可以通过常规的方括号表示法来读取或写入。

"use strict";

const arr_4 = [0, 2, 4, 6, 8];
alert(arr_4[3]);  // 6
arr_4[0] = 9;
alert(arr_4); // 9, 2, 4, 6, 8

当你访问一个超出数组实际长度的元素时,数组的大小会增长,并且新元素会被创建。

"use strict";

const arr_5 = [0, 2, 4, 6, 8];
arr_5[10] = 9;
alert(arr_5); // 0, 2, 4, 6, 8,,,,,,9
alert(arr_5.length); // 11

不同数据类型

你可以在数组中存储不同数据类型的值。

"use strict";

const arr_6 = [0, "two", 4]; // 数字和字符串
console.log(arr_6);        // [0, "two", 4]

// 甚至可以存储类型为“数组”的值
const arr_7 = [10, 11];
arr_7[2] = arr_6;          // 数组中的数组
console.log(arr_7);        // [10, 11, [0, "two", 4]]
console.log(arr_7.length); // 3

嵌套数组

如前所示,数组的元素可以是数组(而这些数组本身也可以包含数组类型的元素)。这种情况可以在运行时或初始化时发生。要直接访问更深的层级,你必须指定足够多的方括号对 [] 来达到该层级。

"use strict";

const arr_8 = [ [0, 1], [10, 11, 12, 13], [20, 21] ];
console.log(arr_8[2]);    // 一层级访问到数组: [20, 21]
console.log(arr_8[1][1]); // 两层级访问到一个数字: 11

// 同样适用于赋值操作 ...
arr_8[2][0] = "twenty";
console.log(arr_8[2]); // ["twenty", 21]

更复杂一点

"use strict";

const arr_9 = []; // 空数组
arr_9[0] = [];
arr_9[0][0] = [];
arr_9[0][0][2] = "Hallo world!";
console.log(arr_9);  // [[[undefined, undefined, "Hallo world!"]]]

arr_9[2] = "Third element of first level";
console.log(arr_9);
// [[[undefined, undefined, "Hallo world!"]], undefined, "Third element of first level"]

属性和方法

length

length 是每个数组的一个属性(不是方法)。它表示数组中元素的数量。

alert([0, 1, 2].length);  // 3

请注意,数组索引是从 0 开始的。因此,数组的长度要比最后一个索引大。

concat

concat 方法返回两个或多个数组的组合。要使用它,首先需要两个或更多的数组进行组合。

const arr1 = ["a", "b", "c"];
const arr2 = ["d", "e", "f"];

然后,创建一个第三个数组,并将其值设置为 arr1.concat(arr2)

const arr3 = arr1.concat(arr2); // arr3 现在是: ["a", "b", "c", "d", "e", "f"]

请注意,在这个例子中,新的 arr3 数组包含了 arr1arr2 数组的内容。

join 和 split

join 方法返回一个包含数组中所有元素的单一字符串,这些元素由指定的分隔符分隔。如果没有指定分隔符,则默认使用逗号。

还有一个 split 方法,它执行 join 的反向操作:它作用于一个字符串,根据指定的分隔符将字符串分割为多个元素,并返回一个包含这些元素的数组。(提示:split 是字符串数据类型的方法,而不是数组的方法。)

首先,创建一个数组。

const abc = ["a", "b", "c"];

然后,创建一个新变量并将其赋值为 abc.join()

const a = abc.join();  // "a,b,c"

你还可以使用自定义的分隔符。

// 使用分号加空格作为分隔符
const b = abc.join("; ");  // "a; b; c"

然后,可以使用字符串的 split 方法将其转换回数组。

const a2 = a.split(",");  // ["a", "b", "c"]
const b2 = b.split("; "); // ["a", "b", "c"]

push

push 方法将一个或多个元素添加到数组的末尾,并返回数组的新长度。

"use strict";
const arr = [0, 1, 2, 3];

alert(arr);                // 0, 1, 2, 3
const len = arr.push(100);
alert(len);                // 5
alert(arr);                // 0, 1, 2, 3, 100

pop

pop 方法删除数组的最后一个元素并返回该元素。

"use strict";
const arr = [0, 1, 2, 3];

alert(arr);                // 0, 1, 2, 3
const elem = arr.pop();
alert(elem);               // 3
alert(arr);                // 0, 1, 2

pushpop 在数组的末尾操作;它们的效果是相反的。

unshift

unshift 方法将一个或多个新元素添加到数组的开头,并返回数组的新长度。它通过“移动”每个旧元素从其旧索引 ii + 1,然后将新元素添加到索引 0,并更新数组的长度属性。它与 push 类似,但作用于数组的开头。

"use strict";
const arr = [0, 1, 2, 3];

alert(arr);                // 0, 1, 2, 3
const len = arr.unshift(100);
alert(len);                // 5
alert(arr);                // 100, 0, 1, 2, 3

shift

shift 方法删除数组的第一个元素并返回该元素。它通过“移动”每个旧元素从其旧索引 ii - 1,然后更新数组的长度属性,最后返回被移除的第一个元素。它与 pop 类似,但作用于数组的开头。

"use strict";
const arr = [0, 1, 2, 3];

alert(arr);                // 0, 1, 2, 3
const elem = arr.shift();
alert(elem);               // 0
alert(arr);                // 1, 2, 3

unshiftshift 在数组的开头操作;它们的效果是相反的。

最后修改: 2025年01月13日 星期一 15:03