1:map:通过指定的函数处理数组的每个元素 然后返回处理后的数组
const numbers = [1, 2, 3, 4];
const doubled = numbers.map(num => num * 2); // [2, 4, 6, 8]
2:push:尾部添加
const fruits = ['apple', 'banana'];
fruits.push('orange'); // ['apple', 'banana', 'orange']
3:pop:尾部删除
const numbers = [1, 2, 3, 4];
numbers.pop(); // [1, 2, 3]
4:shift:头部删除
const numbers = [1, 2, 3, 4];
numbers.shift(); // [2, 3, 4]
5:unshift:头部添加
const fruits = ['apple', 'banana'];
fruits.unshift('orange'); // ['orange', 'apple', 'banana']
6:reverse:颠倒
const numbers = [1, 2, 3, 4];
numbers.reverse(); // [4, 3, 2, 1]
7:concat:合并
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const combined = array1.concat(array2); // [1, 2, 3, 4, 5, 6]
8:join:转换成字符串
const fruits = ['apple', 'banana', 'cherry'];
const result = fruits.join(', '); // "apple, banana, cherry"
9:toString:转字符串
const numbers = [1, 2, 3];
const result = numbers.toString(); // "1,2,3"
10:JSON.stringify:转换成JSON数据
const person = { name: 'John', age: 30 };
const jsonString = JSON.stringify(person); // '{"name":"John","age":30}'
11:forEach:遍历数组
const numbers = [1, 2, 3];
numbers.forEach(num => console.log(num)); // 1 2 3
12:filter:过滤数组
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(num => num % 2 === 0); // [2, 4]
13:some:数组求和
const numbers = [1, 2, 3, 4];
const hasEven = numbers.some(num => num % 2 === 0); // true
14:split:以指定字符分割数组,返回新数组
const str = "apple,banana,cherry";
const fruits = str.split(','); // ['apple', 'banana', 'cherry']
15:splice:截取,2个参数:删除 3个参数:添加(开始,删除数量,插入数量)
- 删除元素:
const fruits = ['apple', 'banana', 'cherry'];
fruits.splice(1, 1); // 删除索引为1的元素,结果:['apple', 'cherry']
- 添加元素:
const fruits = ['apple', 'cherry'];
fruits.splice(1, 0, 'banana'); // ['apple', 'banana', 'cherry']
16:slice:截取(开始位置,数量)
const numbers = [1, 2, 3, 4, 5];
const sliceResult = numbers.slice(1, 4); // [2, 3, 4]
17:sort:排序
const numbers = [4, 1, 3, 2];
numbers.sort(); // [1, 2, 3, 4]
ヾ( ̄▽ ̄)Bye~Bye~
ጿ ኈ ቼ ዽ ጿ ኈ ቼ ዽ ጿ ኈ ቼ ዽ ጿ ኈ ቼ ዽ ጿ ኈ ቼ