js array (Array, arr) commonly used methods

1. The common methods of arr are as follows

        1. arr.push() ==> Add to the end of the array

        2. arr.pop() ==> delete an item at the end of the array

        3. arr.unshift() ==> Add to the first position of the array

        4. arr.shift() ==> delete the first item in the array

        5. arr.forEach() ==> loop array

        6. arr.map() ==> loop array

        7. arr.reverse() ==> reverse the order of elements in the array

        8. arr.concat() ==> concatenate two arrays

        9. arr.splice() ==> delete, insert and replace array

        10. arr.slice() ==> intercept array

        11. arr.sort() ==> sort array

        12. arr.join(',') ==> change the array to, (comma separated) string

        13. arr.toString() ==> Change array to string

        14. arr.find() ==> Find an item in the array that satisfies the condition

        15. arr.findIndex() ==> Find the index of the first item in the array that meets the conditions

        16. arr.includes() ==> Find whether there is an item in the array

        17. arr.reduce() ==> can be used as an accumulator

2. Detailed introduction

        1. arr.push()

                - Function: add to the end

                - Whether to accept parameters: support the input of multi-digit parameters, add one when one is passed in, and add multiple digits when multiple digits are passed in

                - Whether to operate the source array: Yes

                - Return: return the added data

                - eg:

let arr = [1,2,3]

arr.push(4)
console.log(arr) // [1,2,3,4]

arr.push(5,6)
console.log(arr) // [1, 2, 3, 4, 5, 6]

arr.push(7,8,9)
console.log(arr) // [1, 2, 3, 4, 5, 6, 7, 8, 9]

2. arr.pop()

        - Function: delete an item at the end of the array

        - Whether to accept parameters: do not accept parameters

        - Whether to operate the source array: yes

        - Return: return the last deleted item of data

        - eg:

let arr = [1, 2, 3]

arr.pop()

console.log(arr) //[1, 2]

3. arr.unshift()

        - Function: add to the first place

        - Whether to accept parameters: support the input of multi-digit parameters, add one when one is passed in, and add multiple digits when multiple digits are passed in

        - Whether to operate the source array: yes

        - Return: return the added data

        - eg:

let arr = [1, 2, 3]

arr.unshift(5, 6)

console.log(arr) // [5, 6, 1, 2, 3]

4. arr.shift()

        - Function: to delete the first item of the array

        - Whether to accept parameters: do not accept parameters

        - Whether to operate the source array: yes

        - Return: returns the data of the first item removed

        - eg:

let arr = [1, 2, 3]

arr.shift()

console.log(arr) //  [2, 3]

5. arr.forEach()

        - Function: loop array (operate on each item of the array)

        - Whether to accept parameters: accept a function (item, index, arr) => {console.log('item, i, arr', item, i, arr);}

                item: the current item of the loop

                i: the index of the current item of the loop

                arr: the array itself

        - Whether to operate the source array: yes

        - Returns: undefined

        - eg:

let arr = [1, 2, 3];

let result = arr.forEach((item, i, arr) => {
  console.log('item, i, arr', item, i, arr);
  // item, i, arr 1 0 [1, 2, 3]
  // item, i, arr 2 1 [1, 2, 3]
  // item, i, arr 3 2 [1, 2, 3]
  item = item + 1;
});

console.log(arr); // [2, 3, 4]
console.log(result); // undefined

6. arr.map()

        - Function: loop array (operate on each item of the array)

        - Whether to accept parameters: accept a function (item, index, arr) => {console.log('item, i, arr', item, i, arr);}

                item: the current item of the loop

                i: the index of the current item of the loop

                arr: the array itself

        - Whether to operate the source array: no

        - Return: *** returns the new array after processing (return must be used)

        - eg:

let arr = [1, 2, 3];

let result = arr.map((item, i, arr) => {
  console.log('item, i, arr', item, i, arr);
  // item, i, arr 1 0 [1, 2, 3]
  // item, i, arr 2 1 [1, 2, 3]
  // item, i, arr 3 2 [1, 2, 3]
  return item + 1;
});

console.log(arr); // [1, 2, 3]
console.log(result); // [2, 3, 4]

7. arr.reverse()

        - Function: Reverse the order of elements in an array

        - Whether to accept parameters: No

        - Whether to operate the source array: yes

        - Return: *** returns the new array after processing

        - eg:

let arr = [1, 2, 3, 4, 5];

let result = arr.reverse();

console.log('arr', arr);  // [5, 4, 3, 2, 1]
console.log('result', result); // [5, 4, 3, 2, 1]

8. arr.concat()

        - Function: concatenate two arrays

        - Whether to accept parameters: accept arrays, multiple

        - Whether to operate the source array: no

        - Return: *** returns the new array after processing

        - eg:

let arr = [1, 2, 3, 4, 5];

let result = arr.concat([6, 7], [8, 9]);

console.log('arr', arr);  // [1, 2, 3, 4, 5]
console.log('result', result);  // [1, 2, 3, 4, 5, 6, 7, 8, 9]

9. arr.splice()

        - Function: Delete, insert and replace arrays are all possible (two parameters are interception, more than three parameters are replacement, when there are more than three parameters, the length of the second parameter interception is 0, which is addition)

        - Whether to accept parameters: accept at least two parameters arr.splice(index, len, ...others)

                index: index, start replacing from that index

                len: the intercepted length

                ...others: the content to be added after interception (it can be countless parameters, just add a few parameters if you add parameters)

        - Whether to operate the source array: yes

        - Return: the intercepted part

        - eg:

// 截取
let arr = [1, 2, 3, 4, 5];

let result = arr.splice(1, 2);
console.log('arr', arr); // [1, 4, 5]

console.log('result', result); // [2, 3]


// 替换
let arr = [1, 2, 3, 4, 5];

let result = arr.splice(1, 2, 9, 9, 9, 9, 9);

console.log('arr', arr); // [1, 9, 9, 9, 9, 9, 4, 5]
console.log('result', result); // [2, 3]


// 添加
let arr = [1, 2, 3, 4, 5];

let result = arr.splice(1, 0, 9, 9, 9, 9, 9);

console.log('arr', arr); // [1, 9, 9, 9, 9, 9, 2, 3, 4, 5]
console.log('result', result); // []

10. arr.slice()

        - Function: intercept array

                When there is only one parameter, start from startIndex and intercept to the end;

                parameter can be negative

        - Whether to accept parameters: accept at least one parameter arr.slice(startIndex, endIndex)

                startIndex: the index to start intercepting

                endIndex: intercepted to that index (the intercepted item does not contain endIndex)

        - Whether to operate the source array: no

        - Return: an array composed of parts after interception

        - eg:

// eg1
let arr = [1, 2, 3, 4, 5];

let result = arr.slice(1);

console.log('arr', arr); // [1, 2, 3, 4, 5]
console.log('result', result); // [2, 3, 4, 5]


// eg2
let arr = [1, 2, 3, 4, 5];

let result = arr.slice(1, 3);

console.log('arr', arr); // [1, 2, 3, 4, 5]
console.log('result', result); // [2, 3]


// eg3
let arr = [1, 2, 3, 4, 5];

let result = arr.slice(-3, -1);

console.log('arr', arr); // [1, 2, 3, 4, 5]
console.log('result', result); // [3, 4]

11. arr.sort() 

        - Function: sorting arrays (arrays, strings, and Chinese characters can all be sorted) (according to ASCII code sorting)

        - Whether to accept parameters: can accept parameters, accept a function (a, b) => a - b

                Ascending order: (a, b) => a - b / no reference

                Descending order: (a, b) => b - a

        - Whether to operate the source array: yes

        - Return: returns the sorted array

        - eg:

// eg1 升序
let arr = [4, 5, 7, 2, 3, 7, 2, 1];

let result = arr.sort();

console.log('arr', arr); // [1, 2, 2, 3, 4, 5, 7, 7]
console.log('result', result); // [1, 2, 2, 3, 4, 5, 7, 7]


// eg2 升序
let arr = [4, 5, 7, 2, 3, 7, 2, 1];

let result = arr.sort((a, b) => a - b);

console.log('arr', arr); // [1, 2, 2, 3, 4, 5, 7, 7]
console.log('result', result); // [1, 2, 2, 3, 4, 5, 7, 7]


// eg3 降序
let arr = [4, 5, 7, 2, 3, 7, 2, 1];

let result = arr.sort((a, b) => {
    return b - a
});

console.log('arr', arr); // [7, 7, 5, 4, 3, 2, 2, 1]
console.log('result', result); // [7, 7, 5, 4, 3, 2, 2, 1]

12. arr.join(param)

        - Function: Change the array to a string separated by any character (param)

        - Whether to accept parameters: accept a character

        - Whether to operate the source array: no

        - Return: Returns the *string* after splitting

        - eg:

// eg1 逗号分隔
let arr = [1, 2, 3, 4, 5];

let result = arr.join(',');

console.log('arr', arr); // [1, 2, 3, 4, 5]
console.log('result', result); // 1,2,3,4,5


// eg2 中线
let arr = [1, 2, 3, 4, 5];

let result = arr.join('-');

console.log('arr', arr); // [1, 2, 3, 4, 5]
console.log('result', result); // 1-2-3-4-5


// eg3 数字1
let arr = [1, 2, 3, 4, 5];

let result = arr.join(1);

console.log('arr', arr); // [1, 2, 3, 4, 5]
console.log('result', result); // 112131415

13. arr.toString()

        - Function: change array to string

        - Whether to accept parameters: No

        - Whether to operate the source array: no

        - Return: Returns the *string* after splitting

        - eg:

let arr = [1, 2, 3, 4, 5];

let result = arr.toString();

console.log('arr', arr); // [1, 2, 3, 4, 5]
console.log('result', result); // 1,2,3,4,5

14. arr.find()

        - Function: Find an item in the array that satisfies the condition

        - Whether to accept parameters: accept a function (item) => item == 8

        - Whether to operate the source array: no

        - Return: If found, return the first item that satisfies, if not found, return undefined

        - eg:

let arr = [1, 2, 3, 4, 5];

let result = arr.find(item => item == 8});

console.log('arr', arr); // [1, 2, 3, 4, 5]
console.log('result', result); // undefined

15. arr.findIndex()

        - Function: Find the index of the first item in the array that satisfies the condition

        - Whether to accept parameters: accept a function (item) => item == 8

        - Whether to operate the source array: no

        - Return: find and return the index of the first item that satisfies, and return -1 if not found

        - eg:

let arr = [1, 2, 3, 4, 5];

let result = arr.findIndex(item => item == 8});

console.log('arr', arr); // [1, 2, 3, 4, 5]
console.log('result', result); // -1

16. arr.includes()

        - Function: Find if there is an item in the array

        - Whether to accept parameters: accept a function

        - Whether to operate the source array: no

        - Return: true if found, false if not found

        - eg:

let arr = [1, 2, 3, 4, 5];

let result = arr.findIndex(5});

console.log('arr', arr); // [1, 2, 3, 4, 5]
console.log('result', result); // true

17. arr.reduce()

        - Function: can be used as accumulator, deduplication, etc.

        - Whether to accept parameters: accept two parameters arr.reduce((pre, curr) => {}, total)

                First parameter: function(pre, curr) => {}

                        pre: the value returned by the last loop,

                        curr: current cycle item

                The second parameter: the initialized word

        - Whether to operate the source array: no

        - Return: returns the value of the loop end processing

        - eg:

let arr = [1, 2, 3, 4, 5];

let result = arr.reduce((per, curr) => {
  return per + curr;
}, 0);

console.log('arr', arr); // [1, 2, 3, 4, 5]
console.log('result', result); // 15

Guess you like

Origin blog.csdn.net/snows_l/article/details/130622838