So what I have is an array, like following
[[1,2],[2,3],[3,4]]
I want to do the computation on the array without changing the original array elements.
i tried slice(), Array.from() and [...arr] also.
The issue is that the original array also changes with the new array.
Please suggest what is missing in the code below.
var arr = [
[1, 2],
[2, 3],
[3, 4]
];
function clicked() {
var scale = 1.1;
var newArray = arr.slice();
newArray.forEach(element => {
var x = element[0];
var y = element[1];
newX = x * scale;
newY = y * scale
element[0] = newX;
element[1] = newY;
});
console.log('New Array: ', newArray);
console.log('Old Array: ', arr);
}
document.querySelector('#click').addEventListener('click', function() {
clicked();
});
<button class="click" id="click"> Click me and check Console</button>