数组

48 阅读4分钟

数组常用方法

  1. arr.push(element1, ..., elementN)
    push()方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度。
    
  2. arr.pop()
    pop()方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度。
    
  3. arr.shift()
    shift()方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度。
    
  4. arr.unshift(element1, ..., elementN)
    unshift()方法将一个或多个元素添加到数组的开头,并返回该数组的新长度(该方法修改原有数组)。
    
  5. arr.slice([begin[, end]])
    slice()方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。
    
  6. let new_array = old_array.concat(value1[, value2[, ...[, valueN]]])
    concat()方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
    
  7. array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
    splice()方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。
    
  8. arr.every(callback(element[, index[, array]])[, thisArg])
    every()方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。
    
  9. arr.some(callback(element[, index[, array]])[, thisArg])
    some()方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。
    
  10. let newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
    filter()方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。 
    
  11. const array1 = [1, 4, 9, 16];
    // pass a function to map
    const map1 = array1.map(x => x * 2);
    console.log(map1);
    // expected output: Array [2, 8, 18, 32]
    map()方法创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值。
    
  12. const values = [1, 3, 4, 6, 7, 4, 3, 1];
    values.forEach((item, index, array) => {
      array[index] = item * 2;
    });
    console.log(values); // [ 2, 6, 8, 12, 14, 8, 6, 2 ]
    forEach()方法对数组的每个元素执行一次给定的函数。
    
  13. join & split
    const colors = 'green, red, black';
    const colorsArr = colors.split(',');
    console.log(colorsArr); // [ 'green', ' red', ' black' ]
    const colorsStr = colorsArr.join(',');
    console.log(colorsStr); // green, red, black
    
  14. reverse & sort
    
    const values = [1, 3, 44, 43, 654, 0];
    values.reverse();
    console.log(values); // [ 0, 654, 43, 44, 3, 1 ]
    values.sort();
    console.log(values); // [ 0, 1, 3, 43, 44, 654 ] 首字母开始比较
    values.sort((val1, val2) => val2 - val1);
    console.log(values); // [ 654, 44, 43, 3, 1, 0 ]
    
  15. indexOf()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1
  16. arr.lastIndexOf(searchElement[, fromIndex])
    lastIndexOf() 方法返回指定元素(也即有效的 JavaScript 值或变量)在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找,从 fromIndex 处开始。
    
  17. reduce & reduceRight
    
    const values = [1, 3, 4, 4, 4, 9];
    const sum = values.reduce((prev, cur, index, array) => {
      return prev + cur;
    });
    console.log(sum); // 25
    
    const sumRight = values.reduceRight((prev, cur, index, array) => {
      return prev + cur;
    });
    console.log(sumRight); // 25
    

es6

  1. 拓展运算符...
    const colors = ['green', 'red', 'pink'];
    const colors1 = ['white', 'grey'];
    const colors2 = [...colors, ...colors1];
    console.log(colors2); // [ 'green', 'red', 'pink', 'white', 'grey' ]
    
  2. Array.from() 方法对一个类似数组或可迭代对象创建一个新的,浅拷贝的数组实例。
    console.log(Array.from('foo'));
    // expected output: Array ["f", "o", "o"]
    console.log(Array.from([1, 2, 3], x => x + x));
    // expected output: Array [2, 4, 6]
    
  3. Array.of() 方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。
    console.log(Array.of(undefined, 1, null)); // [ undefined, 1, null ]
    
  4. copyWithin(target, start = 0, end = this.length)
    const arr = [1, 3, 4, 4, 5, 8, 10, 1, 0].copyWithin(0, 3, 4);
    console.log(arr); // [ 4, 3, 4, 4, 5, 8, 10, 1, 0 ]
    const arr = [1, 3, 4, 4, 5, 8, 10, 1, 0].copyWithin(0, 3, 8);
    console.log(arr1); // [ 4, 5, 8, 10, 1, 8, 10, 1, 0 ]
    
  5. fill() 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。
    const colors = ['green', 'red', 'pink'];
    const colors1 = colors.fill('white');
    console.log(colors1); // [ 'white', 'white', 'white' ] 
    
  6. find & findIndex
    const values = [1, 3, 4, 5, 6, NaN];
    const findResult = values.find(num => num > 4 ); // 找不到为undefined
    console.log(findResult); // 5
    const findIndexResult = values.findIndex(num => num > 4 ); // 找不到为-1
    console.log(findIndexResult); // 3
    
  7. entries(), keys() & values()
    const colors = ["red", "green", "blue"];
    for (const index of colors.keys()) {
      console.log(index); // 0 1 2
    }
    
    for (const ele of colors.values()) {
      console.log(ele); // red green blue
    }
    for (const [index, ele] of colors.entries()) {
      console.log(index, ele);
    }
    // 0 red
    // 1 green
    // 2 blue
    

es7

  • includes 第二个参数是搜索的起始位置
    const values = [1, 3, 4, 5, 6, NaN];
    console.log(values.includes(NaN)); // true
    console.log(values.includes(4, 3)); // false
    

es10

  • flat & flatMap
    const values = [1, 2, [3, 4]].flat();
    console.log(values); // [1, 2, 3, 4]
    const valuesDeep = [1, [2, [3]]];
    console.log(valuesDeep.flat(Infinity)); // [1, 2, 3]