3

I have given array of strings and I'd want to make map which will have those strings as keys and values will be how many times each string is present in the array. The problem is I can't really increment value, which was once added to the map. It's my code:

for(let str of data) {
    if(map.has(str)) map[str] = map.get(str) + 1;
    else map.set(str, 1);
}

But it doesn't work as intended. I tried map[str]++ etc. but nothing worked for me...

1 Answer 1

4

You're almost there:

for(const str of data) {
    if(map.has(str)) map.set(str, map.get(str) + 1);
    else map.set(str, 1);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.