Front-end interview JS—common methods of arrays

Table of contents

1. Operation (add/delete/modify/check)

increase

delete

change

check

2. Sorting

3. Conversion

4. Iteration


1. Operation (add/delete/modify/check)

The basic operations of arrays include add, delete, modify, and search. You need to pay attention to which methods will affect the original array and which methods will not.

increase

The first three methods below are addition methods that affect the original array, while the fourth method will not affect the original array.

  • push() - add to the end of the array
  • unshift() - add any number of values ​​to the beginning of the array
  • splice()——Pass in three parameters, namely the starting position, 0 (the number of elements to be deleted), and the inserted element
  • concat() - (copy) creates a copy of the current array and then adds its parameters to the end of the copy

delete

The following three will affect the original array, and the last one does not affect the original array:

  • pop()——Deletes the last item of the array, simultaneously reduces the array's length value, and returns the deleted item (similar to popping the stack)
  • shift() - used to delete the first item of the array, while reducing the array's length value and returning the deleted item
  • splice()——Pass in two parameters, namely the starting position and the number of deleted elements, and return an array containing the deleted elements.
  • slice() - Creates a new array containing one or more elements of the original array, without affecting the original array
  • let colors = ["red", "green", "blue", "yellow", "purple"];
    let colors2 = colors.slice(1);
    let colors3 = colors.slice(1, 4);
    console.log(colors)   // red,green,blue,yellow,purple
    concole.log(colors2); // green,blue,yellow,purple
    concole.log(colors3); // green,blue,yellow

change

is to modify the content of the original array. Commonly used splice—— is to pass in three parameters, namely the starting position, the number of elements to be deleted, any number of elements to be inserted, and the deleted elements are returned. array, affecting the original array

check

That is, find the element and return the element coordinates or element value

  • indexOf()——Returns the position of the element to be found in the array, or -1 if not found
  • includes()——Returns the position of the element to be found in the array. If found, returntrue, otherwisefalse
  • find() - returns the first matching element

2. Sorting

Arrays have two methods that can be used to reorder elements:

  • reverse()——Reverse the array
  • sort() - accepts a comparison function to determine which value should be sorted first
function compare(value1, value2) {
    if (value1 < value2) {
        return -1;
    } else if (value1 > value2) {
        return 1;
    } else {
        return 0;
    }
}
let values = [0, 1, 5, 10, 15];
values.sort(compare);
alert(values); // 0,1,5,10,15

3. Conversion

Common conversion methods are:

  • join() - accepts one parameter (string delimiter) and returns a string containing all items
let colors = ["red", "green", "blue"];
alert(colors.join(",")); // red,green,blue
alert(colors.join("||")); // red||green||blue

4. Iteration

Commonly used methods to iterate arrays (without changing the original array) are as follows:

  • some()
  • every()
  • forEach() - runs the passed function on each item of the array, no return value
  • filter()
  • map() - runs the passed function on each item of the array and returns an array consisting of the results of each function call

Guess you like

Origin blog.csdn.net/qq_62799214/article/details/134840976