数组,再熟悉不过的数据类型,自己抽时间做一个整理,归类,了解操作API,才能更好运用

    大致的整理归类为2种类型: 1) 会修改原来数组 2)不会修改原来数组

    会修改原来数组的常用方法

    1. Array.prototype.pop() 从数组中删除最后一个元素并返回该元素。
    1. pop 删除数组中的最后一个元素并返回该元素。 此方法更改数组的长度
    2. 讲讲pop的具体用法及兼容性
    3. var plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']
    4. console.log(plants.pop()); // "tomato“
    1. Array.prototype.push() 将一个或多个元素添加到数组的末尾并返回数组的长度
    1. let a = [1];
    2. console.log(a.push(2)); //返回数组长度, 会改变a的原始长度
    3. // 元素可以累添加; a.push(3,4,5,6);
    4. console.log(a) // [1,2];
    1. Array.prototype.sort() 对数组的元素进行排序并返回数组。
    1. arr.sort([compareFunction[firstE1, secondE2]])
    2. compareFunction 比较的方法, 指定定义排序顺序的函数。
    3. 如果省略,则将数组元素转换为字符串,然后根据每个字符的Unicode代码点值进行排序。
    4. let numbers = [4, 2, 5, 1, 3];
    5. numbers.sort(function(a, b) {
    6. return a - b;
    7. });
    8. console.log(numbers); [1, 2, 3, 4, 5]
    1. Array.prototype.shift() 从数组中删除第一个元素并返回该元素。

    返回值: 从数组中删除的元素; undefined如果数组为空, 否则返回删除的元素值
    会改变原数组。

    1. // 该shift()方法从数组中删除第一个元素并返回已删除的元素。此方法更改数组的长度。
    2. let array1 = [1, 2, 3];
    3. let firstElement = array1.shift();
    4. console.log(array1);
    5. // expected output: Array [2, 3]
    6. console.log(firstElement);
    7. // expected output: 1
    1. Array.prototype.splice() 对数组的元素进行排序并返回数组

    通过删除或替换现有元素和/或在适当位置添加新元素更改数组的内容。
    array .splice(start [,deleteCount [,item1 [,item2 [,…]]]]
    start: 要开始更改数组的索引,如果大于数组的长度,start则将设置为数组的长度
    deleteCount: 一个整数,指示要从中删除的数组中的元素个数。
    item1, item2… 要添加到数组的元素,从 start。如果未指定任何元素,splice()则只会从数组中删除元素。
    返回值:包含已删除元素的数组。如果仅删除一个元素,则返回一个元素的数组。如果未删除任何元素,则返回空数组。

    1. let months = ['Jan', 'March', 'April', 'June'];
    2. months.splice(1, 0, 'Feb'); // 没有删除元素,则返回一个空数组 []
    3. // inserts at index 1
    4. console.log(months);
    5. // expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']
    6. months.splice(4, 1, 'May');
    7. // replaces 1 element at index 4
    8. console.log(months);
    9. // expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']
    1. Array.prototype.unshift()() 将一个或多个元素添加到数组的前面,并返回数组的新长度 返回值: 返回数组的长度 ```javascript let array1 = [1, 2, 3];

    console.log(array1.unshift(4, 5)); // expected output: 5

    console.log(array1); // expected output: Array [4, 5, 1, 2, 3]

    1. 7. Array.prototype.copyWithin() 复制数组中的一系列数组元素 ()
    2. 8. Array.prototype.fill() 使用静态值从开始索引到结束索引填充数组的所有元素。
    3. 不会修改数组并返回数组的某些表示形式<br />arr .fillvalue [,start [ end]])f<br />value: 填充数组的值。<br />start: 启动索引,默认为0。<br />end: 结束索引,默认为`this.length`
    4. ```javascript
    5. [1, 2, 3].fill(4); // [4, 4, 4]
    6. [1, 2, 3].fill(4, 1); // [1, 4, 4]
    7. [1, 2, 3].fill(4, 1, 2); // [1, 4, 3]
    8. [1, 2, 3].fill(4, 1, 1); // [1, 2, 3]
    9. [1, 2, 3].fill(4, 3, 3); // [1, 2, 3]
    10. [1, 2, 3].fill(4, -3, -2); // [4, 2, 3]
    11. [1, 2, 3].fill(4, NaN, NaN); // [1, 2, 3]
    12. [1, 2, 3].fill(4, 3, 5); // [1, 2, 3]
    13. Array(3).fill(4); // [4, 4, 4]
    14. [].fill.call({ length: 3 }, 4); // {0: 4, 1: 4, 2: 4, length: 3}

    不会修改数组长度

    1. Array.prototype.concat() 返回一个新数组,该数组是与其他数组和/或值连接的数组。

    返回值: 一个新的Array实例。

    1. const letters = ['a', 'b', 'c'];
    2. const numbers = [1, 2, 3];
    3. letters.concat(numbers);
    4. // result in ['a', 'b', 'c', 1, 2, 3]
    1. Array.prototype.reverse() 反转数组元素的顺序 - 第一个成为最后一个,最后一个成为第一个。

    返回值: 反转后的对象

    1. let a = [1,2,3];
    2. console.log(a.reverse()); //得到的是原来数组的值 [3,2,1]
    3. console.log(a); // [1,2,3]
    1. Array.prototype.indexOf() 返回数组中元素的第一个(最小)索引,该索引等于指定的值,如果找不到,则返回-1。

    2. Array.prototype.join() 将数组的所有元素连接成一个字符串。

    3. Array.prototype.lastIndexOf() 返回数组中元素的最后一个(最大)索引,该索引等于指定值,如果找不到,则返回-1。
    4. Array.prototype.slice() 提取数组的一部分并返回一个新数组。

    arr .slice([ begin [,end ]])
    begin:
    从零开始的索引,开始提取。可以使用负索引,指示距序列末尾的偏移量。slice(-2)提取序列中的最后两个元素。如果begin未定义,slice则从索引开始0。如果begin大于序列的长度,则返回空数组
    end
    基于零的索引,之前结束提取。slice提取但不包括end。例如,slice(1,4)通过第四个元素(元素索引为1,2和3)提取第二个元素。可以使用负索引,指示距序列末尾的偏移量。slice(2,-1)通过序列中倒数第二个元素提取第三个元素。如果end省略,则slice提取序列的末尾(arr.length)。如果end大于序列的长度,则slice提取到序列的末尾(arr.length)。

    返回值: 包含提取元素的新数组。

    1. const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
    2. const citrus = fruits.slice(1, 3);
    3. // fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
    4. // citrus contains ['Orange','Lemon']
    1. Array.prototype.toSource() 返回表示指定数组的数组文字; 您可以使用此值来创建新数组

    2. Array.prototype.toString() 返回表示数组及其元素的字符串。

    1. const array1 = [1, 2, 'a', '1a'];
    2. console.log(array1.toString());
    3. // expected output: "1,2,a,1a"
    1. Array.prototype.toLocalString() 返回表示数组及其元素的本地化字符串。