Expand the role of grammar (... role)

  1. When the function has multiple parameters, use the expanded array to pass the parameters
function sum(a,b,c){
    
    
return a + b + c; 
}
var arr = [1,2,3]
console.log(sum(...arr))  // output 6
  1. Copying the array does not affect the original array (only one-dimensional array and no objects in the array)
var arr1 = [1,2]
var arr2 = [...arr1]
arr2[0]=[2]
console.log(arr1) // output[1,2]
  1. Concatenate multiple arrays
var arr1 = [1,2,3]
var arr2 = [3,4,5]
var arr3 = [7,8,9]
var arr = [...arr1,...arr2,...arr3]
console.log(arr)  // output [1,2,3,4,5,6,7,8,9] 

Reference
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_syntax

Guess you like

Origin blog.csdn.net/qq_26889291/article/details/112464336