Why the splice mode is different after the array is deconstructed, and the resulting array is different.

let list = ["1", "2"]

let list1 = [...list].splice(0, 1)
let list2 = [...list]
list2.splice(0, 1)

console.log(list1) // ["1"]
console.log(list2) // ["2"]

Why does the chained call get the wrong result?

Dec.26,2021

the location where http://www.w3school.com.cn/js.
splice( adds / removes items, the number of items to delete, the new items added to the array), and then returns the deleted items.
let list1 = [.list] .splice (0,1) delete 1 from position 0, list1 = deleted items
list2.splice (0,1) delete 1 from position 0, list2 has the first item left, list2 = [2]

Menu