Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const objectsArray =
- [
- {
- "id": 1,
- "first_name": "Mark",
- "last_name": "Harrison",
- "email": "mharrison0@g.co",
- "gender": "Male",
- "ip_address": "247.64.88.56"
- }, {
- "id": 2,
- "first_name": "Carol",
- "last_name": "Bradley",
- "email": "cbradley1@edublogs.org",
- "gender": "Female",
- "ip_address": "30.85.44.102"
- }, {
- "id": 3,
- "first_name": "Carolyn",
- "last_name": "Webb",
- "email": "cwebb2@alexa.com",
- "gender": "Female",
- "ip_address": "204.43.122.95"
- }, {
- "id": 4,
- "first_name": "Todd",
- "last_name": "Rodriguez",
- "email": "trodriguez3@zdnet.com",
- "gender": "Male",
- "ip_address": "115.218.218.156"
- }, {
- "id": 5,
- "first_name": "Scott",
- "last_name": "Simpson",
- "email": "ssimpson4@marketwatch.com",
- "gender": "Male",
- "ip_address": "95.33.253.249"
- }, {
- "id": 6,
- "first_name": "Frank",
- "last_name": "Johnston",
- "email": "fjohnston5@marketwatch.com",
- "gender": "Male",
- "ip_address": "153.46.102.52"
- }, {
- "id": 7,
- "first_name": "John",
- "last_name": "Moore",
- "email": "jmoore6@blogs.com",
- "gender": "Male",
- "ip_address": "105.173.113.212"
- }, {
- "id": 8,
- "first_name": "Jacqueline",
- "last_name": "Parker",
- "email": "jparker7@go.com",
- "gender": "Female",
- "ip_address": "167.105.134.135"
- }, {
- "id": 9,
- "first_name": "Aaron",
- "last_name": "Lawrence",
- "email": "alawrence8@google.pl",
- "gender": "Male",
- "ip_address": "52.8.77.22"
- }, {
- "id": 10,
- "first_name": "Mary",
- "last_name": "Thomas",
- "email": "mthomas9@washington.edu",
- "gender": "Female",
- "ip_address": "119.66.85.117"
- }
- ]
- const countCharacters = function (arr, prop) {
- const count = arr.filter(item => item.hasOwnProperty(prop))
- .map(item => item[prop])
- .reduce((acc, val) => {
- val = String(val).toLowerCase()
- for (const item of val) {
- acc.hasOwnProperty(item)
- ? acc[item]++
- : acc[item] = 1
- }
- return acc
- }, {})
- console.log(count)
- let mostFrequent
- for (const prop in count) {
- if (!mostFrequent) {
- mostFrequent = {character: prop, number: count[prop]}
- } else {
- if (count[prop] > mostFrequent.number) {
- mostFrequent = {character: prop, number: count[prop]}
- }
- }
- }
- return mostFrequent.character
- }
- countCharacters(objectsArray, 'first_name')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement