Collate and share some common methods used in arrays


Outline:

  1. How to empty an array
  2. How do I convert an array to a string
  3. How to convert an array to a specified format string
  4. How do I concatenate two arrays into one
  5. How do I reverse the contents of an array summary
  6. How to intercept the contents of a specified range in an array
  7. How do I find the position of an element in an array
  8. How do I determine if an array contains an element
  9. How to convert a string to an array

How to empty an array

arr = []

    let arr = [1.2.3.4.5];
    arr = [];
    console.log(arr);   / / []
Copy the code

arr.length = 0

    let arr = [1.2.3.4.5];
    arr.length = 0;
    console.log(arr);   / / []
Copy the code

arr.splice(0,arr.length)

    let arr = [1.2.3.4.5];
    arr.splice(0,arr.length);
    console.log(arr);   / / []
Copy the code

All three methods empty the array and change the original array


How do I convert an array to a string

arr.toString()

    let arr = [1.2.3.4.5];
    let str = arr.toString();
    console.log(arr);   / / [1, 2, 3, 4, 5]
    console.log(str);   / / 1, 2, 3, 4, 5
Copy the code

str = arr + ”

    let arr = [1.2.3.4.5];
    let str = arr + ' ';
    console.log(arr);   / / [1, 2, 3, 4, 5]
    console.log(str);   / / 1, 2, 3, 4, 5
Copy the code

str = arr.join();

    let arr = [1.2.3.4.5];
    let str = arr.join();   // join if no argument is passed, use comma (,) by default
    console.log(arr);   / / [1, 2, 3, 4, 5]
    console.log(str);   / / 1, 2, 3, 4, 5
Copy the code

All three methods convert an array to a string and return a new string without changing the original array


How to convert an array to a specified format string

arr.join()

    let arr = [1.2.3.4.5];
    let str = arr.join(); // join if no argument is passed, use comma (,) by default
    console.log(arr);   / / [1, 2, 3, 4, 5]
    console.log(str);   / / 1, 2, 3, 4, 5
Copy the code

arr.join(“-“)

    let arr = [1.2.3.4.5];
    let str = arr.join("-"); 
    console.log(arr);   / / [1, 2, 3, 4, 5]
    console.log(str);   / / the 1-2-3-4-5
Copy the code

The join method converts the array to a string. If no argument is passed, the default is a comma (,). If the argument is passed, it is delimited by the argument symbol and does not change the array


How do I concatenate two arrays into one

arr1.concat(arr2)

    let arr1 = [1.3.5.7];
    let arr2 = [2.4.6.8];
    console.log(arr1);  / / hc-positie [1]
    console.log(arr2);  / /,4,6,8 [2]
    let res = arr1.concat(arr2);
    console.log(res);   //[1, 3, 5, 7, 2, 4, 6, 8]
Copy the code

res = […arr1,…arr2]

    let arr1 = [1.3.5.7];
    let arr2 = [2.4.6.8];
    console.log(arr1);  / / hc-positie [1]
    console.log(arr2);  / /,4,6,8 [2]
    let res = [...arr1,...arr2];    //es6 extends the operator
    console.log(res);   //[1, 3, 5, 7, 2, 4, 6, 8]
Copy the code

The extension operator, on the right hand side of the equal sign, unwinds all the data in the array and puts it where it belongs

Either way, you can concatenate two arrays into one and return a new array without modifying the original array


How do I reverse the contents of an array summary

arr.reverse()

    let arr = [1.3.5.7.9];
    arr.reverse(); 
    console.log(arr);   //[9, 7, 5, 3, 1]
Copy the code

If you reverse the array, you change the original array


How to intercept the contents of a specified range in an array

arr.slice(1, 3)

    let arr = [1.3.5.7.9];
    let res = arr.slice(1.3);
    console.log(arr);   //[1, 3, 5, 7, 9]
    console.log(res);   / / [3, 5];
Copy the code

Note: slice is a method that uses a header but not a tail, meaning that when a slice is truncated, it contains argument 1 but not argument 2. Slice 1 = start slice 2 = end slice does not change the original array and returns a new array


How do I find the position of an element in an array

arr.indexOf()

If indexOf finds an element

    let arr = [1.3.5.7.9];
    let res = arr.indexOf(3);
    console.log(res);   //[1] returns the subscript
Copy the code

If indexOf does not find an element

    let arr = [1.3.5.7.9];
    let res = arr.indexOf(0);
    console.log(res);   //[-1] returns the subscript
Copy the code

If there are two identical values in the array, I want to look for the second one (default is left to right, return if found).

    let arr = [1.3.5.7.9.3];
    let res = arr.indexOf(3);
    console.log(res);   //[1] returns the subscript
Copy the code

If I must use indexOf to find the second, then I need to use the second parameter of indexOf

    let arr = [1.3.5.7.9.3];
    let res = arr.indexOf(3.2);
    console.log(res);   //[5] returns the subscript
Copy the code

IndexOf parameter 1 = the element to search for indexOf parameter 1 = the index from which to search for indexOf IndexOf if an element is found = returns the indexOf the found element


arr.lastIndexOf()

    let arr = [1.3.5.7.9];
    let res = arr.lastIndexOf(3);
    console.log(res);   //[1] returns the subscript
Copy the code

LastIndexOf is used the same way as indexOf, except that indexOf starts from left to right. LastIndexOf starts from right to left


How do I determine if an array contains an element

IndexOf and lastIndexOf

    let arr = [1.3.5.7.9];
    let res = arr.indexOf(3);
    console.log(res);   / / 1
Copy the code
    let arr = [1.3.5.7.9];
    let res = arr.lastIndexOf(3);
    console.log(res);   / / 1
Copy the code
    let arr = [1.3.5.7.9];
    let res = arr.indexOf(0);
    console.log(res);    / / 1
Copy the code
    let arr = [1.3.5.7.9];
    let res = arr.lastIndexOf(0);
    console.log(res);   / / 1
Copy the code

Use indexOf and lastIndexOf to find an array, returning the index if the element is present and -1 if it is not

arr.includes()

    let arr = [1.3.5.7.9];
    let res = arr.includes(1);
    console.log(res);  //true
Copy the code
    let arr = [1.3.5.7.9];
    let res = arr.includes(0);
    console.log(res);  //fasle
Copy the code

Use arr.includes() to find if the element is present in the array and return true if so, if not. Returns false


How to convert a string to an array

split()

    let str = "1,3,5,7,9";
    let arr = str.split(",");
    console.log(arr); //["1", "3", "5", "7", "9"]
Copy the code

The split method is the opposite of the join method. Split converts a string to an array and join converts an array to a string. Both methods are split by parameters


Check out my personal blog?