Common methods of JS array

Posted by powerspike on Sun, 23 Jan 2022 02:03:56 +0100

[will change the original array]

1. Array.push(), adds one or more elements to the end of the array and returns the new array length. Original array change

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

2. Array.pop(), delete and return the last element of the array, if the array is empty Ze returns undefind. Original array change

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

3. Array.unshift(), adds one or more elements to the beginning of the array and returns the new array length. Original array change

let arr = [1,2,3,4]
let res = arr.unshift('0','Hello')
console.log(res)  // 6
consloe.log(arr)  // ['0', 'hello', 1,2,3,4]

4. Array.shift(), deletes the first item of the array and returns the value of the first element If the cover array is empty, it returns undefined. Original array change

let arr = ['Hello',1,2,3,4]
let res = arr.shift()
console.log(res)  // Hello
console.log(arr)  // [ 1, 2, 3, 4]

5. Array.revrse(), flashback the array Change the original array

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

6. Array.sort() sorts the array UniCode code by string

① Sort from small to large

let arr = [1, 2, 99, 67, 88]
let sortNum = function (a, b) { return a-b }
console.log(arr.sort(sortNum))  // [1, 2, 67, 88, 99]

② Sort from large to small

let arr = [1, 2, 99, 67, 88]
let sortNum = function (a, b) { return b-a }
console.log(arr.sort(sortNum))  // [99, 88, 67, 2, 1]

③ Sort by a value in an array object

let arr = [
    {name:'Zhang San',age:'18'},
    {name:'Li Si',age:'14'},
    {name:'Wang Wu',age:'26'}
]

function compare(param){
    return function sortAge(a,b){
        return a[param]-b[param]
    }
}
console.log(arr.sort(compare('age')))

7. Array.splice(index,howmany,arr1,arr2...), Delete and add elements, delete howmany elements from the index position, and add Arr1, arr2 The data is inserted successively from the index position When howmany is 0, the element is not deleted and the deleted array is returned

The original array has changed

let arry = ['1','2','3','4','5','6']
console.log(arry.splice(2,2,'test','ceshi'))  // ['3', '4']
console.log(arry)  // ['1', '2', 'test', 'ceshi', '5', '6']

 

[original array does not change]

1. Array.join(), connect each item of the array into a string with the specified string The default connector is "," comma

let arr = [1,2,3,4]
let str1 = arr.join()
let str2 = arr.join('-')
console.log(str1)  // 1,2,3,4
console.log(str2)  // 1-2-3-4

2. Array.concat(arr1,arr2...), Merge two or more arrays to generate a new array without changing the original array

let arr = [1,2,3,4]
let arr1 = ['a','b','c']
let arr2 = ['A','B','C']
let res = arr.concat(arr1,arr2)
console.log(res)  // [1, 2, 3, 4, 'a', 'b', 'c', 'A', 'B', 'C']

3.Array.map(function), after each item of the original array executes the function, it returns a new array The original array does not change

Note the difference with forEach

4. Array. Foreach (function) is used to call each element of the array and pass the element to the callback function The original array does not change

(note that Array.forEach is printed directly, and the result is undefind)

5. Array.slice(start,end) starts from start and ends before end If the end value is not given, it starts from start to the end of the array Start can be given a negative value, - 1 represents the last position of the array, - 2 represents the penultimate, and so on

let arry = [1,2,3,4]
console.log(arry.slice(-3,-1))  // [2, 3]

6. Array.filter(function), filter the array, and return a new array of qualified elements The original array does not change

let arr = [1,2,3,4,5]
console.log(arr.filter(x=>x>3))  // [4, 5]
console.log(arr)  // [1, 2, 3, 4, 5]

7. Array.every(function) to judge each item in the array. If all items match, it returns true; otherwise, it returns false

8. Array.some(function) to judge each item in the array. If it does not meet the requirements, it will return false; otherwise, it will return true

9. Array.reduce(function). The reduce() method receives a function as an accumulator. Each value in the array (from left to right) is reduced and finally calculated as a value

let arr = [1,2,3,4,5]
let aa = ['1',2,3,4,5]
console.log(arr.reduce((a,b)=>a+b))  // 15
console.log(aa.reduce((a,b)=>a+b))  // 12345

Topics: Javascript ECMAScript