How do I reverse an array in JavaScript while preserving the original value of the original array? -
this question has answer here:
i'm trying reverse order of array using .reverse() method in javascript, trying preserve original order of elements in original array. when save values variable, end transposing elements in original array creating new 1 same format. eloquent way perform task?
var arrayone = [1,2,3,4,5]; var arraytwo = arrayone.reverse(); //arraytwo = [5, 4, 3, 2, 1] //arrayone = [5, 4, 3, 2, 1]
var arraytwo = arrayone.slice().reverse();
slice clone array
Comments
Post a Comment