javascript 数组常用方法总结

1.push()

push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。末尾添加,返回的是长度,会改变原数组

2. pop()

pop() 方法用于删除并返回数组的最后一个元素。返回最后一个元素,会改变原数组

3. shift()

shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。返回第一个元素,改变原数组

4. unshift()

unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。返回新长度,改变原数组

5. concat()

concat() 方法用于连接两个或多个数组。该方法不会改变现有的数组,仅会返回被连接数组的一个副本。

6. join()

join() 方法用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的,默认使用’,'号分割,不改变原数组

7. slice(start,end)

截取一个新的数组,包含从 start 到 end (不包括该元素)的 原数组的浅拷贝。该方法不会修改原数组

slice(-2) 表示提取原数组中的倒数第二个元素到最后一个元素(包含最后一个元素)。

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
//只有一个参数时默认是start,一直到最后一个(包含最后一个)
console.log(animals.slice(2));
//["camel", "duck", "elephant"]

8. splice()

splice() 方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。

从索引为start的位置开始删除deleteCount个元素,插入[item1,item2...]

array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

9. substring() 和 substr()

相同点:如果只是写一个参数,两者的作用都一样:都是是截取字符串从当前下标以后直到字符串最后的字符串片段。
substr(startIndex);
substring(startIndex);

不同点:第二个参数
substr(startIndex,lenth): 第二个参数是截取字符串的长度(从起始点截取某个长度的字符串);
substring(startIndex, endIndex): 第二个参数是截取字符串最终的下标 (截取2个位置之间的字符串,‘含头不含尾’)。

10. sort 排序

按照 Unicode code 位置排序,默认升序。可以在参数里指名排序方法

var numbers = [4, 2, 5, 1, 3];
numbers.sort((a, b) => a - b);//b-a就是降序
console.log(numbers);

// [1, 2, 3, 4, 5]

11. reverse()

reverse() 方法用于颠倒数组中元素的顺序。返回的是颠倒后的数组,会改变原数组

12. indexOf 和 lastIndexOf

都接受两个参数:查找的值、查找起始位置。
不存在,返回 -1 ;存在,返回位置。
indexOf 是从前往后查找, lastIndexOf 是从后往前查找。

13. every

every() 方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。

14. some

some() 方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。

const array = [1, 2, 3, 4, 5];
const even = (element) => element % 2 === 0;
console.log(array.some(even));  //true
console.log(array.every(even)); //false

15. filter

对数组的每一项都运行给定的函数,返回 结果为 ture 的项组成的数组

示例:

const numbers = [1, 2, 3, 4, 5, 6];
const result = numbers.filter(number => number > 4);
console.log(result);
// [5,6]

16. map

对数组的每一项都运行给定的函数,返回每次函数调用的结果组成一个新数组

示例:

const array1 = [1, 4, 9, 16];
const map1 = array1.map(x => x * 2);
console.log(map1);
//  [2, 8, 18, 32]

17. reduce 

对数组元素从左到右依次执行function函数,然后返回单个累计的值

语法

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

参数

function (执行数组中每个值的函数,包含四个参数)

    1、total (必需。初始值, 或者计算结束后的返回值)
    2、currentValue (必需。当前元素)
    3、index (可选。当前元素的索引)
    4、array (可选。当前元素所属的数组对象)

initialValue (可选。传递给函数的初始值)

 示例:

const arr = [1, 2, 3, 4];
const reducer = (total, currentValue) => total + currentValue;

// 1 + 2 + 3 + 4
console.log(arr.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(arr.reduce(reducer, 5));
// expected output: 15

18. forEach 数组遍历

19. find()

传入一个回调函数,找到数组中符合当前搜索规则的第一个元素,返回它,并且终止搜索。

示例:

const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
// 12

20. findIndex()

传入一个回调函数,找到数组中符合当前搜索规则的第一个元素,返回它的下标,终止搜索。

21. fill()

用新元素替换掉数组内的元素,可以指定替换下标范围。

22. from

将类似数组的对象(array-like object)和可遍历(iterable)的对象转为真正的数组

23. of

用于将一组值,转换为数组。这个方法的主要目的,是弥补数组构造函数 Array() 的不足。因为参数个数的不同,会导致 Array() 的行为有差异。

24. includes

判断数组中是否存在该元素,参数:查找的值、起始位置,可以替换 ES5 时代的 indexOf 判断方式。
indexOf 判断元素是否为 NaN,会判断错误。

你可能感兴趣的:(javascript,开发语言,ecmascript)