I am currently facing an issue with the code snippet provided below.
var array = [1, 3, 2]
var newArray = []
getNewArray() {
for (let i = 0; i < array.length; i++) {
for (let x = 0; x < array[i]; x++) {
this.newArray.push(array[i]);
}
}
console.log(this.newArray);
}
My goal is to iterate through the numbers in an array based on their values, producing results like the following:
(3) [{…}, {…}, {…}]
0:
count:(1) [1]
1:
count:(3) [1,2,3]
2:
count:(2) [1,2]
However, the current output I am getting is different from what I expected:
(4) [1, 2, 2, 1]
0: 1
1: 2
2: 2
3: 1