Please, try to understand where is a mistake.
I have an array table
and two functions appendRow
and appendCol
.
If I execute appendCol
first it works correct and in each of array in table 'y' element is appended.
But, If I execute first 'appendRow', and then appendCol
'y' is added twice two first and last array in table.
I do not understand why.
My code is bellow:
var table = [
['x', 'x', 'x', 'x'],
['x', 'x', 'x', 'x'],
['x', 'x', 'x', 'x'],
['x', 'x', 'x', 'x'],
['x', 'x', 'x', 'x']
]
var appendRow = () => {
var newTable = table;
newTable.push(newTable[0])
table = newTable
console.log(table)
}
var appendCol = () => {
var newTable = table;
newTable.forEach((element) => {
element.push("y")
})
table = newTable;
console.log(table);
}
appendRow();
appendCol();
Link to jsfiddle
You can see console after executing.
Try to comment first function and second works correct