I am trying to use a map to count duplicate string number in an array, my code:
var map = {};
var myarray = ["John", "John", "John", "Doe", "Doe", "Smith",
"John", "Doe", "Joe"];
for (var a = 0; a < myarray.length; a++) {
if (map[myarray[a]] !== null) {
map[myarray[a]] += 1;
} else {
map[myarray[a]] = 1;
}
}
but when I do console.log(map); it returns
Object {John: NaN, Doe: NaN, Smith: NaN, Joe: NaN}
Why it is NaN? I want to have the result like:
John :4, Doe: 3, Smith: 1, Joe: 1
How can I do? Thank you in advance.