Does a map update automatically if the original array is updated?

Viewed 68

I am creating an array in a for loop and using a map to keep track of the number of times I've used certain values (e.g. key: count; "AB": 2, "BC": 3). I've declared the empty array and created the map for that, outside the loop.

Inside the loop, I'm updating the array based on the value in the map ( if (map.get(x) < 3) {array[i] = x}).

I'm assuming the condition isn't working because I'm not updating the map, but is there a way to have the map update automatically as the array is updated?

var array = [];
var map = array.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

for (var i = 0; i < 24; i++) {

    var random = Math.floor(Math.random() * 10)
    if (map.get(random) < 3) {
    array[i] = random
    }

}
0 Answers
Related