var alph = ["a", "b", "c"];
var r = [];
for(var i = 0; i < 5; i += 1) {
r.push(alph);
}
r[0].reverse();
console.log(r);
/* Output
[ [ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ],
[ 'c', 'b', 'a' ] ]
*/
/* Expected output
[ [ 'c', 'b', 'a' ],
[ 'a', 'b', 'c' ],
[ 'a', 'b', 'c' ],
[ 'a', 'b', 'c' ],
[ 'a', 'b', 'c' ] ]
*/
There are arrays in an array. The first array should be reversed. I thought r[0].reverse()
would do this, but instead this reverses all arrays.
Can someone explain why this happens ?