Merge array array.push.apply()

Obj. Func. Apply (obj, argv).

The first argument to the Apply method is the scope in which the function is run, and the second argument is the argument passed to the function, which can be an array or arguments object

Var arr1 = [1, 2]; Var arr2 = (three, four, five); arr2.push.apply(arr2,arr1); / / [1, 2, 3, 4, 5]Copy the code

Call the apply method on the arr2.push instance, passing in arr1 as an argument, and then the arr2.push method will traverse all the elements of the arR1 array, storing the elements of arR1 into the ARR2 array and changing the ARR2 array.

eg1:

a=[]; A.paush ([1,2,3]), then you get [[1,2,3]]

[].push. Apply (a,[1,2,3]), then you get [1,2,3]

Eg2: Merges two arrays into a new array

Const arr1 = [{"deleted_at": null}, {" creATED_AT ": "2021-07-22T05:54:17.000z ",}]; const arr2 = [ { "url": "https://music.163.com/#/artist?id=2116", }, { "type": 200, } ]; let arry = []; / / method 1: 'arry. Push (... arr1, ... arr2); // Method 2: arry.push. Apply (arry,arr1) arry.push. Apply (arry,arr2) console.log(arry) // Print values: [{deleted_at: Null}, {creATED_at: '2021-07-22T05:54:17.000z'}, {url: 'https://music.163.com/#/artist?id=2116'}, {type: 200}]Copy the code