I have created a function that is designed to merge multiple multidimensional arrays together
The input 'table' is currently 2 arrays contained in objects, the function is designed to run each time a command is run(not very efficient I know, but it is for a highschool project where this is required)
Printing the input looks like:
[ [ [ 'a', 1 ], [ 'b', 1 ] ], [ [ 'a', 1 ] ] ]
Expecting the resulting table to look like this every time I would run the function:
[ [ 'a', 2 ], [ 'b', 1 ] ]
Instead of incrementing 'tempTable[u][1]' it increments the array connected to table[0] of the input.
Printing the original array which was [ [ 'a', 1 ], [ 'b', 1 ] ] would now show 'a' incrementing by 1 each time the code was run.
I have no idea why it is incrementing the original array instead of just 'tempArray' each time, would somebody be able to explain to me why it increments the original table?
P.S. It is the fault of this function, if I comment out the line that should increment 'tempTable' by 1, the original table doesn't change.
function combineTable(table) {
var tempTable = table[0];
for (var i = 1; i < table.length; i++) { //each table
for (var u = 0; u < tempTable.length; u++) { //temptable values
for (var y = 0; y < table[i].length; y++) { //current table values
if (tempTable[u][0] === (table[i])[y][0]) {
tempTable[u][1] += (table[i])[y][1];
} else {
console.log("Not contained");
}
}
}
}
}
combineTable([
[
['a', 1],
['b', 1]
],
[
['a', 1]
]
])