Seven ways to change the original array in JS

Table of contents

One, push

 Two, pop

Three, unshift

Four, shift

Five, splice

Six, sort

Seven, reverse


One, push

Adds an element to the end of the array and returns the new length.

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

 Two, pop

Deletes the last element of the array and returns the deleted element.

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

Three, unshift

Adds an element to the beginning of the array and returns the new length.

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

Four, shift

Deletes the first element of the array and returns the deleted element.

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

Five, splice

 Used to delete, add, and replace some elements in an array; returns an array of deleted or replaced elements.

1), add

let arr = [1, 2, 3, 4, 5]
let arr1 = arr.splice(2, 0, 'haha')
console.log(arr1)  //[1, 2, 'haha', 3, 4, 5]新增一个元素

2), delete

let arr = [1, 2, 3, 4, 5]
let arr2 = arr.splice(2, 3)
console.log(arr2)  //[1, 2] 删除三个元素

3), replace

let arr = [1, 2, 3, 4, 5]
let arr3 = arr.splice(2, 1, 'haha')
console.log(arr3)  //[1, 2, 'haha', 4, 5] 替换一个元素

Six, sort

Sorts the elements of an array and returns the sorted array.

  • basic use
let arr = [1,5,3,7,6];
let n = arr.sort();
console.log(arr); // [1, 3, 5, 6, 7]
console.log(n); // [1, 3, 5, 6, 7]
  • Look at the following example
let arr = [6,8,1,30,5];
arr.sort();
console.log(arr); // [1, 30, 5, 6, 8]

Looking at the results of the above sorting is not feeling and strange, not the result of my own imagination. In fact, when no parameters are passed in the array sortmethod, the sorting order defaults to converting the data to be sorted into strings and sorting them according to the sequence; so the default sorting Unicodehere is not sorting by value, but to achieve sorting by value sortYou need to pass in a comparison function. This function compares the magnitudes of two values ​​and returns a number describing the relative order of the two values. Specifically, the sorting principle is the insertion sort and quick sort in the v8 engine, and the source code of the v8 engine sorting . When the length of the array is less than or equal to 10, use insertion sort, and when the length is greater than 10, use quick sort.

The comparison function has two parameters a and b, and its return value is as follows:

If a is less than b, that is, a - b is less than zero, a value less than zero is returned, and the array will be sorted in ascending order.

If a is equal to b, 0 is returned, and the order of the array remains unchanged.

If a is greater than b, that is, a - b is greater than zero, then return a value greater than zero, and the array will be sorted in descending order.

let arr = [1,5,3,7,6];
arr.sort((a,b) => 0);
console.log(arr); // [1, 5, 3, 7, 6]
arr.sort((a,b) => a - b);
console.log(arr); // [1, 3, 5, 6, 7]
arr.sort((a,b) => b - a);
console.log(arr); // [7, 6, 5, 3, 1]

Seven, reverse

Reverses the order of elements in an array and returns a new array.

let arr = [1, 2, 3, 4];
arr.reverse()
console.log(arr)  // [4, 3, 2, 1]

Guess you like

Origin blog.csdn.net/qq_52421092/article/details/130513269