Created
August 1, 2019 05:02
-
-
Save sudhir600/c65d7b41ef820b69d5baa393548436eb to your computer and use it in GitHub Desktop.
Get total numbers of same key in array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// E.g | |
// let array = ["sudhir", "hello", "test", "sudhir", "a", "a", "a"] | |
//output = sudhir:2, hello:1, test: 1 and a:3 | |
countKey(array){ | |
let obj = {} | |
array.forEach(function(i){ | |
if(obj[i] == undefined){ | |
obj[i] = 0 | |
} | |
obj[i] = obj[i] + 1 | |
}) | |
return obj | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment