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

后端 未结 22 1932
佛祖请我去吃肉
佛祖请我去吃肉 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
    2020-11-22 02:50

    Fast ways to duplicate an array in JavaScript in Order:

    #1: array1copy = [...array1];

    #2: array1copy = array1.slice(0);

    #3: array1copy = array1.slice();

    If your array objects contain some JSON-non-serializable content (functions, Number.POSITIVE_INFINITY, etc.) better to use

    array1copy = JSON.parse(JSON.stringify(array1))

提交回复
热议问题