Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. const objectsArray =
  2. [
  3. {
  4. "id": 1,
  5. "first_name": "Mark",
  6. "last_name": "Harrison",
  7. "email": "mharrison0@g.co",
  8. "gender": "Male",
  9. "ip_address": "247.64.88.56"
  10. }, {
  11. "id": 2,
  12. "first_name": "Carol",
  13. "last_name": "Bradley",
  14. "email": "cbradley1@edublogs.org",
  15. "gender": "Female",
  16. "ip_address": "30.85.44.102"
  17. }, {
  18. "id": 3,
  19. "first_name": "Carolyn",
  20. "last_name": "Webb",
  21. "email": "cwebb2@alexa.com",
  22. "gender": "Female",
  23. "ip_address": "204.43.122.95"
  24. }, {
  25. "id": 4,
  26. "first_name": "Todd",
  27. "last_name": "Rodriguez",
  28. "email": "trodriguez3@zdnet.com",
  29. "gender": "Male",
  30. "ip_address": "115.218.218.156"
  31. }, {
  32. "id": 5,
  33. "first_name": "Scott",
  34. "last_name": "Simpson",
  35. "email": "ssimpson4@marketwatch.com",
  36. "gender": "Male",
  37. "ip_address": "95.33.253.249"
  38. }, {
  39. "id": 6,
  40. "first_name": "Frank",
  41. "last_name": "Johnston",
  42. "email": "fjohnston5@marketwatch.com",
  43. "gender": "Male",
  44. "ip_address": "153.46.102.52"
  45. }, {
  46. "id": 7,
  47. "first_name": "John",
  48. "last_name": "Moore",
  49. "email": "jmoore6@blogs.com",
  50. "gender": "Male",
  51. "ip_address": "105.173.113.212"
  52. }, {
  53. "id": 8,
  54. "first_name": "Jacqueline",
  55. "last_name": "Parker",
  56. "email": "jparker7@go.com",
  57. "gender": "Female",
  58. "ip_address": "167.105.134.135"
  59. }, {
  60. "id": 9,
  61. "first_name": "Aaron",
  62. "last_name": "Lawrence",
  63. "email": "alawrence8@google.pl",
  64. "gender": "Male",
  65. "ip_address": "52.8.77.22"
  66. }, {
  67. "id": 10,
  68. "first_name": "Mary",
  69. "last_name": "Thomas",
  70. "email": "mthomas9@washington.edu",
  71. "gender": "Female",
  72. "ip_address": "119.66.85.117"
  73. }
  74. ]
  75.  
  76. const countCharacters = function (arr, prop) {
  77.  
  78. const count = arr.filter(item => item.hasOwnProperty(prop))
  79. .map(item => item[prop])
  80. .reduce((acc, val) => {
  81. val = String(val).toLowerCase()
  82. for (const item of val) {
  83. acc.hasOwnProperty(item)
  84. ? acc[item]++
  85. : acc[item] = 1
  86. }
  87. return acc
  88. }, {})
  89. console.log(count)
  90.  
  91. let mostFrequent
  92. for (const prop in count) {
  93. if (!mostFrequent) {
  94. mostFrequent = {character: prop, number: count[prop]}
  95. } else {
  96. if (count[prop] > mostFrequent.number) {
  97. mostFrequent = {character: prop, number: count[prop]}
  98. }
  99. }
  100. }
  101. return mostFrequent.character
  102. }
  103.  
  104. countCharacters(objectsArray, 'first_name')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement