Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop

后端 未结 22 1492
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 02:10

In order to duplicate an array in JavaScript: which of the following is faster to use?

###Slice method

var dup_array = original_array.slice();
<         


        
22条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 02:35

    You can follow this code. Immutable way array clone. This is the perfect way to array cloning

    
    const array = [1, 2, 3, 4]
    
    const newArray = [...array]
    newArray.push(6)
    console.log(array)
    console.log(newArray)
    

提交回复
热议问题