查找
方法 | 概述 | 语法 | 返回值 |
---|---|---|---|
find() | 返回数组中满足提供的测试函数的第一个元素的值。 | arr.find(callback(element, index, array)[, thisArg]) | 第一个value,否则undefined。 |
findIndex() | 返回数组中满足提供的测试函数的第一个元素的索引。 | arr. findIndex(callback(element, index, array)[, thisArg]) | 第一个index,否则-1(区别于find)。 |
indexOf() | 返回在数组中可以找到一个给定元素的第一个索引。 | arr.indexOf(searchElement[, fromIndex = 0]) | 第一个index,否则-1。 |
lastIndexOf() | 返回指定元素在数组中的最后一个的索引 | arr.lastIndexOf(searchElement[, fromIndex = arr.length - 1]) | 最后一个index,否则-1。 |
includes() | 判断一个数组是否包含一个指定的值。 | arr.includes(searchElement[, fromIndex = 0]) | Boolean |
遍历
方法 | 概述 | 语法 | 返回值 |
---|---|---|---|
every() | 测试数组的所有元素是否都通过了指定函数的测试。 | arr.every(callback(element, index, array)[, thisArg]) | Boolean |
some() | 测试数组中的某些元素是否通过由提供的函数实现的测试。 | 测试数组中的某些元素是否通过由提供的函数实现的测试。 | Boolean |
forEach() | 对数组的每个元素执行一次提供的函数。 | array.forEach(callback(currentValue, index, array){//do something}, this) | undefined |
filter() | 创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。 | arr.filter(callback(element, index, array)[, thisArg]) | 新数组 |
map() | 创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。 | arr.map(function callback(currentValue, index, array) { // Return element for new_array }[, thisArg]) | 新数组 |
reduce() | 对累加器和数组中的每个元素(从左到右)应用一个函数,将其减少为单个值。 | arr.reduce(callback(accumulator, currentValue, currentIndex, array)[, initialValue]) 如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错。 |
函数累计处理的结果 |
拷贝
方法 | 概述 | 语法 | 返回值 |
---|---|---|---|
slice() | 返回一个从开始到结束(不包括结束)选择的数组的一部分浅拷贝到一个新数组对象。原始数组不会被修改。 | arr.slice([begin, end]) | 含有提取元素的新数组 |
增删
方法 | 概述 | 语法 | 返回值 |
---|---|---|---|
pop() | 删除最后一个元素。此方法直接修改原数组。 | arr.pop() | 被删除的元素(当数组为空时返回undefined)。 |
push() | 将一个或多个元素添加到数组的末尾。 | arr.push(element1, ..., elementN) | 原数组的新length |
shift() | 删除第一个元素。此方法直接修改原数组。 | arr.shift() | 被删除的元素(当数组为空时返回undefined)。 |
unshift() | 将一个或多个元素添加到数组的开头。此方法直接修改原数组。 | arr.unshift(element1, ..., elementN) | 原数组的新length |
splice() | 除现有元素和/或添加新元素 | array.splice(start[, deleteCount, item1, item2, ...]) | 被删除的元素组成的新数组。如果没有,返回空数组。 |
排序
方法 | 概述 | 语法 | 返回值 |
---|---|---|---|
reverse() | 倒序排序 | arr.reverse() | 倒序排列后的原数组 |
sort() | 顺序排序 | arr.sort([compareFunction]) compareFunction用来指定按某种顺序进行排列的函数。如果省略,元素按照转换为的字符串的各个字符的Unicode位点进行排序。 |
返回排序后的数组。原数组已经被排序后的数组代替。 |
网友评论