Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  82] [ 6]  / answers: 1 / hits: 136462  / 9 Years ago, wed, june 3, 2015, 12:00:00

Array.prototype.reverse reverses the contents of an array in place (with mutation)...



Is there a similarly simple strategy for reversing an array without altering the contents of the original array (without mutation)?


More From » arrays

 Answers
16

You can use slice() to make a copy then reverse() it



var newarray = array.slice().reverse();




var array = ['a', 'b', 'c', 'd', 'e'];
var newarray = array.slice().reverse();

console.log('a', array);
console.log('na', newarray);




[#66358] Monday, June 1, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samsons

Total Points: 331
Total Questions: 97
Total Answers: 106

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
;