My program is getting a list of items (a string that is split) and grouping them by the number of "-"
in the start of every phrase, putting them in different levels inside the variable quantityCreatedArray
,
Since this number is changed in every click of a button, I want to store every change in the variable quantityCreatedArray
,
I'm doing this trying to push arrays inside an array using the method Array.push()
,
This whole part is inside a for of
structure that is needed for another part of my code (not important),
The problem is that the array arrayUsed
(that have all arrays from the variable quantityCreatedArray
) is returning an array with arrays that have the last change in the variable quantityCreatedArray
...
Since I'm using the push
method to put the arrays inside arrayUsed
, I tried putting the variables directly in arrayUsed
using a simple for
, yet, the results are the same...
Below is some sample code for you to test
menus = [
"- LEVEL 1",
"- - LEVEL1-1",
"- - - LEVEL 1-1-1",
"- - - LEVEL1-1-2",
"- - - - LEVEL1-1-2-1",
"- - - - - LEVEL 1-1-2-1-1",
"- - - - LEVEL-1-2-2",
"- - - LEVEL-1-3",
"- LEVEL2"
];
var quantityCreatedArray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var arrayUsed = [];
for (let item of menus) {
let itemSplited = item.split(" ");
let counter = 0;
while (itemSplited[counter] == "-") {
counter++;
}
for(let i1 = 0;i1<counter;i1++) {
itemSplited.shift()
}
quantityCreatedArray[counter]++
arrayUsed.push(quantityCreatedArray);
}
console.log(arrayUsed);
I expected a record of all changes in the variable quantityCreatedArray
inside arrayUsed
, but instead, I got just the last change in it.