0

I am currently trying to create a function which dynamically changes values of an array in a map. This function is intended to create a new element in the array on each full loop, to represent a new instance to be counted.

The function is meant to count how many visitors to a country there are every day. This is done throgh a map, with the keys being the country and the values being a list of visitors. Each element is how many visitors there were in a specific day.

Where Map:

var countries = new Map(); ('England', [0]); ('France', [0]); ('Spain', [0]); ('Poland', [0]); ('Germany', [0]); ('Russia', [0]); 

For the code:

for(var day = 0; day < daysBack; day++){ var dateToFind = (currentDate.date() + " / " + currentDate.month() + " / " + currentDate.year()); var detectionsOnDate = getActivity(dateToFind ); // Returns an array of locations visited, each visit is a new element // eg. ["France", "France", "England", "France"] for(var detectionLocation of detectionsOnDate){ for(var[location, visits] of countries ){ if(location == detectionLocation){ countries.set(location, visits[day]++); } } } currentDate.subtract(1, 'days'); } 

The result I am looking for is a map similar to:

{ ('England', [242,235,853,122,512]); ('France', [241,621,173,173,512]); ('Spain', [62,235,213,465,126]); ('Poland', [734,163,856,354,142]); ('Germany', [234,235,643,237,512]); ('Russia', [174,200,136,107,164]); } 

In my idea of the logic, this should create a new element in the value every time a new loop of 'day' is done. On each loop of day the same element in the map should be updated.

I'm relatively new to JS so I'm sorry if this is an easy fix, still finding my way around. Any help is appriciated. Thanks.

0

1 Answer 1

2

push returns the length of the array. So, a number will be set to each key. Instead, you can concat a new number to the value array and set it to the current key.

map.set(key, value.concat(value[i]+1)) 

and

map.set(key, value.concat(0)) 
Sign up to request clarification or add additional context in comments.

3 Comments

This is very close to what I am looking for. When this replaces 'map.set(key, value[i]+1);' it creates a new element each time which is a step forward. However, I only would like 'instances' amount of elements (which is no problem, i just make another loop to do that outside of the main one) and for every loop of instances I would like to add 1 each time the if statement is met. I will edit the code above to show what I am looking for.
@FMckinnon it's hard to fix that without the full context tbh. You have 3 nested loops and you are pushing 0 irrespective of any condition. I don't know what getValues returns or how it is connected to instances. And the variable var values of listValues is never used anywhere.
I've created a way around the problem now using a much less efficient way, but I have cleaned up the code and given it context. Feel free to try to find a solution still, I'm invested in the problem now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.