copy of javascript array

1.es6 expansion character (...)

const fruits=['Strawberry','Mango']

const fruitsCopy=[...fruits]  //["Strawberry", "Mango"]

2.es6 Array.from

const fruits = ["Strawberry", "Mango"];

const fruitsCopy2=Array.from(fruits)

// ["Strawberry", "Mango"]

3.slice

const fruits = ["Strawberry", "Mango"];

const fruitsCopy3=fruites.slice()

//["Strawberry", "Mango"]

All built-in array copy operations ( unwind syntax , Array.from() , Array.prototype.slice()  , and  Array.prototype.concat() ) create shallow copies . If you want a deep copy of an array , you can use  JSON.stringify()  to convert the array into a JSON string, then use  JSON.parse()  to convert the string back into a new array completely separate from the original array.

Guess you like

Origin blog.csdn.net/qq_38902432/article/details/130900390
Recommended