js数组方法

map方法
对数组中的每个元素进行某种操作,并返回一个新数组,包含操作后的结果。
map方法不会改变原数组,而是返回一个新数组

1
array.map(callback(item, index, array), thisArg);
  • callback回调函数定义
    • item:数组元素
    • index(可选):数组索引
    • array(可选):调用map的数组
  • thisArg (可选): this

返回新数组


forEach方法
对数组中的某个元素进行某种操作,直接在原数组上进行操作,改变原数组

1
array.foEach(callback(item, index, array), thisArg);

没有返回值(返回undefined


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

1
array.push(element1, element2, ..., elementN)

slice方法
array.slice(start, end);
string.slice(start, end);
左闭右开
用于提取子数组或子字符串(返回新的,不会改变原数组/字符串)


filter方法
用于根据提供的函数测试每个元素,返回一个包含所有通过测试的元素的新数组。filter 方法不会改变原数组。

1
array.filter(callback(item, index, array), thisArg);

js数组方法
http://example.com/2025/04/12/js数组方法/
作者
yvyvSunlight
发布于
2025年4月12日
许可协议