JavaScript 数组
创建, indexing, and iterating 数组, plus 必要映射, 筛选, and reduce 方法.
你将学到
- 创建数组 and accessing 元素通过索引
- 长度属性 and 常见 mutator 方法 (推送, pop, shift, unshift)
- Functional iteration: 映射, 筛选, reduce, forEach
- 搜索使用查找, 一些, 每个, 包含
- Spread 语法 (...) and 数组 destructuring
概念讲解
什么是数组?
An array is an ordered, indexed list of values. In JavaScript, arrays are a special kind of object whose keys are numeric indices.
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // "apple" ← zero-indexed
console.log(fruits.length); // 3
Adding and Removing Elements
const stack = [];
stack.push(""); // add to end → [""]
stack.push("b"); // ["", "b"]
stack.pop(); // remove from end → [""]
const queue = [1, 2, 3];
queue.shift(); // remove from front → [2, 3]
queue.unshift(0); // add to front → [0, 2, 3]
The Big Three: map, filter, reduce
These functional methods are the heart of modern JavaScript data processing. They do not mutate the original array.
映射— transform every element into a new array of the same length.筛选— keep only elements that pass a test.reduce— accumulate all elements into a single value.
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map((n) => n * 2);
// [2, 4, 6, 8, 10]
const evens = numbers.filter((n) => n % 2 === 0);
// [2, 4]
const total = numbers.reduce((sum, n) => sum + n, 0);
// 15
Searching and Testing
const users = [
{ name: "Ada", age: 36 },
{ name: "Alan", age: 41 },
];
users.find((u) => u.name === "Ada"); // { name: "Ada", age: 36 }
users.some((u) => u.age > 40); // true — at least one
users.every((u) => u.age > 30); // true — all of them
[1, 2, 3].includes(2); // true
Spread and Destructuring
const a = [1, 2];
const b = [3, 4];
const combined = [...a, ...b]; // [1, 2, 3, 4]
const [first, second] = [10, 20]; // first=10, second=20
The spread operator ... is also the standard way to copy an array without mutating the original: const copy = [...original];.
示例代码
const products = [
{ name: "Laptop", price: 999, inStock: true },
{ name: "Phone", price: 699, inStock: true },
{ name: "Tablet", price: 399, inStock: false },
{ name: "Monitor", price: 249, inStock: true },
];
// filter: only items in stock
const available = products.filter((p) => p.inStock);
// map: extract just the names
const names = available.map((p) => p.name);
console.log("Available:", names);
// reduce: total price of in-stock items
const totalValue = available.reduce((sum, p) => sum + p.price, 0);
console.log("Total inventory value:", totalValue);
// find: first item over $500
const premium = products.find((p) => p.price > 500);
console.log("First premium item:", premium.name);
// spread to add a new product without mutating the original
const withNew = [...products, { name: "Keyboard", price: 79, inStock: true }];
console.log("Count after adding:", withNew.length);
这个真实示例筛选 in-stock products, 映射到 names, reduces 到总计 price, 查找首先 premium 项, and uses spread 到添加新 product immutably.
动手试一试
- JSON Formatter
数组的 对象是 常见 API 响应 — 格式 and inspect 它们.
- CSV 到 JSON
Convert CSV 数据到 JavaScript 数组的 对象.
常见错误
错误写法
Off-通过-one 错误当 looping 使用 classic for 循环
正确做法
数组是 零-indexed, 所以最后元素是 at 索引长度 - 1. Prefer for...的 or functional 方法 (映射, 筛选) 哪个 avoid manual indexing 完全.
错误写法
Mutating 数组当 你 meant 到复制它
正确做法
方法如 推送, 排序, and splice mutate in 位置. Use spread ([...arr]) or 非-mutating alternatives 如 toSorted() (ES2023) 当你 需要到 preserve 原始.
相关资源
相关工具
- JSON 到 CSV
Convert JavaScript-样式数组的 对象到 CSV.
相关代码片段
- 数组映射 / 筛选 / Reduce
完整引用的 functional 数组方法使用示例.
练习 JavaScript 数组
29 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。