Array methods:

  1. Arr. Splice (start, delete number of elements, add element 1, element 2)Delete array elements (and replaceChange the original array
  2. Arr. Slice (start, end) Returns a new array, copy all array entries from index start to end (excluding end) into a new array
  3. Arr. Concat (arr1 arr2, 1, 2)Splice arrayGenerates a new arrayYou can pass in arrays or values
  4. arr.forEach((item,index,array)=>{})The function runs a function as an argument for each element of the array.This function has no return value

Array search: by index

  1. arr.indexOf(item, from)Search item starting with index from, return index if found, otherwise -1.
  2. arr.lastIndexOf(item, from)Same as above, but search from right to left.
  3. arr.includes(item, from)— Search item from index, return true if found

Note:

  • These methods use strict equality === comparison so if we search for false, it will be true false and not the number zero.
  • If we want to check whether an element is included and don’t want to know the exact index, arr.includes is the first choice.
  • Also, a very small difference in includes is that it handles NaN correctly, unlike indexOf/lastIndexOf

Array search: Searches by specific criteria

  1. arr.find((item,index,array)=>{… }) if true, return item and stop iterating; In the case of falsy, undefined is returned

  2. Arr.findindex (()=>{}) returns the index of the found element, not the element itself, and -1 if nothing is found.

  3. Arr.filter (()=>{}) find returns the first matching element, or an array of all matching elements

Conversion array:

  1. arr.map((item,index,array)=>{})It calls a function on each element of the array,And returns an array of results.
  2. arr.sort(): Applies to arraysIn situIn-place sort
  3. arr.reverse() Invert the elements of the array,Change the original array, also returns the reversed array
  4. str.split(',',3)Divide the string into strings by commas and extract the first three elementsReturn a new array
  5. arr.join(',')Use a commaThe join array returns a string
  6. reduce()accumulatorlet value =arr.reduce(function(accumulator, item, index, array){// ... },[initial]);
let arr = [1.2.3.4.5];
let result = arr.reduce((sum, current) = > sum + current, 0);
alert(result); / / 15
Copy the code

Check if it is an array:

  1. Array.isArray(arr)Returns a Boolean value to distinguish an object from an array
  2. arr.some(fn)
  3. arr.every(fn)The function fn is called for each element of the array. Return true if any/all of the results are true, false otherwise.

other

  1. arr.fill(value, start, end)— From index start to end, populate the array with duplicate values.
  2. arr.copyWithin(target, start, end)— Copies all elements from position start to end to its target location (overwriting existing elements).
  3. arr.flat(depth)/arr.flatMap(fn)Creates a new flat array from a multidimensional array.
    1. Expands a multidimensional array, which returns a new array with no effect on the original data.
    2. The argument to the flat() method is an integer indicating the number of layers you want to flatten. The default is 1, indicating a layer. If you want to convert to a one-dimensional array regardless of the number of nesting levels, you can use the Infinity keyword as an argument
  4. An Array of (for element0 [, element1 elementN [,] [,...]])Create a new Array instance based on a variable number of parameters, regardless of the number or type of parameters
  5. Entries (), keys() and values() methodsAll three methods return oneTraverser objectYou can use for… The of loop iterates

Application of array methods

Through the array

  • When we need to iterate over an array — we can use forEach, for, or for.. Of.
  • When we need to traverse and return data for each element — we can use map.

Array to heavy

Using the Set

let arr = [100.234.2342.1112.100.300.'100'.'hello'.'hello'.NaN.NaN];
let newArr = [...new Set(arr)]; 
Copy the code

Use the reduce + includes

function unique(arr){
  return arr.reduce((prev,cur) = >prev.includes(cur) ? prev :[...prev,cur],[])
}
Copy the code

Copy an array

Shallow copy

  1. Extension operator var newArr = […arr]
  2. Var newArr = arr.concat();
  3. Var newArr = arr.slice(0)

Deep copy

  1. JSON
let obj2=JSON.parse(JSON.stringify(obj1));
Copy the code
  1. The recursive implementation
// Define the function to get the object's constructor (class) name
function getObjectClass(obj) {
    return Object.prototype.toString.call(obj).slice(8, -1)}// Deep-copy functions
function deepClone(obj) {
    // determine whether obj is an object or an array or something else
    if (getObjectClass(obj) === 'Object') {
        var res = {};   // Create an empty object
    } else if (getObjectClass(obj) === 'Array') {
        var res = [];   // Create an empty array
    } else {
        return obj;  // Return the parameter directly
    }
    // Iterate over the passed object
    for (let i in obj) {
        res[i] = deepClone(obj[i]);
    }
    // Returns a new array or object
    return res;
}
Copy the code