Making an independent copy of a reversed array in JavaScript

前端 未结 5 1702
故里飘歌
故里飘歌 2021-01-15 15:50

Here is my fiddle: http://jsfiddle.net/sepoto/Zgu9J/1/

I\'m starting with a reverse function:

function reverseArr(input) {
    var ret = new Array;
          


        
5条回答
  •  粉色の甜心
    2021-01-15 16:30

    All you have to do is clone your array before you reverse it! :)

    The shortest way to type is:

    var b = a.slice();
    

    But using concat is correct as well. See:

    Javascript fastest way to duplicate an Array - slice vs for loop

    If slice does not create a copy for you, you can use numpy arrays and create a deep copy this way:

    np.copy(a);
    

    see: http://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html

提交回复
热议问题