Javascript code:
const bigArray2 = [
[1, 2, 3],
[2, 4, 6],
[3, 6, 9],
];
//creates single-layer array containing all elements of every array within bigArray2
const combinedArray = bigArray2.reduce((accumulated, current) => {
for (element of current) {
accumulated.push(element);
}
return accumulated;
});
console.log(combinedArray);
// outputs [ 1, 2, 3, 2, 4, 6, 3, 6, 9 ] as intended
console.log(bigArray2);
// outputs [ [ 1, 2, 3, 2, 4, 6, 3, 6, 9 ], [ 2, 4, 6 ], [ 3, 6, 9 ] ]
Somehow bigArray2[0] is assigned a value of combinedArray. How and why? .reduce() is not supposed to change the original array