0

i'm looking for a json logic for 2 hours and i haven't found any solution. My json data looks like this:

[
  {
    "_id": "5ae30d020135436357a4732e",
    "balance": "1,171.56",
    "age": 46,
    "eyeColor": "hazel",
    "name": {
      "first": "Colon",
      "last": "Key"
    },
    "company": "Orbaxter",
    "email": "[email protected]",
    "phone": "+1 (938) 402-3422",
    "address": "706 Tennis Court, Kimmell, Indiana, 4057",
    "registered": "2014-12-24T10:31:42 -00:00",
    "latitude": 22.250647,
    "longitude": -67.325278
  },
  {
    "_id": "5ae30d027f167186f52e2e6c",
    "balance": "1,383.36",
    "age": 23,
    "eyeColor": "hazel",
    "name": {
      "first": "Byrd",
      "last": "Walls"
    },
    "company": "Bizmatic",
    "email": "[email protected]",
    "phone": "+1 (817) 569-3406",
    "address": "187 Lewis Avenue, Lutsen, Oregon, 7387",
    "registered": "2017-06-19T09:24:42 -01:00",
    "latitude": -60.642235,
    "longitude": -62.947158
  },
  {
    "_id": "5ae30d023f1f434dcb1ea87d",
    "balance": "2,338.81",
    "age": 21,
    "eyeColor": "grey",
    "name": {
      "first": "Sondra",
      "last": "Booker"
    },
    "company": "Aquazure",
    "email": "[email protected]",
    "phone": "+1 (861) 495-3729",
    "address": "633 Billings Place, Jacumba, Louisiana, 1488",
    "registered": "2017-12-11T08:53:27 -00:00",
    "latitude": 18.681413,
    "longitude": 93.058578
  },
  {
    "_id": "5ae30d02826a978c51641cfb",
    "balance": "2,959.74",
    "age": 40,
    "eyeColor": "hazel",
    "name": {
      "first": "Tammi",
      "last": "Salazar"
    },
    "company": "Terascape",
    "email": "[email protected]",
    "phone": "+1 (906) 405-2566",
    "address": "470 Elliott Walk, Gibbsville, New Mexico, 7298",
    "registered": "2014-08-06T09:06:17 -01:00",
    "latitude": -75.890626,
    "longitude": 19.674008
  },
  {
    "_id": "5ae30d02dd1046807a0e3636",
    "balance": "1,960.08",
    "age": 42,
    "eyeColor": "hazel",
    "name": {
      "first": "Maryellen",
      "last": "Merritt"
    },
    "company": "Digial",
    "email": "[email protected]",
    "phone": "+1 (802) 566-3481",
    "address": "189 Jerome Street, Hoehne, Palau, 8982",
    "registered": "2017-06-18T10:16:48 -01:00",
    "latitude": -16.254906,
    "longitude": 112.500547
  },
  {
    "_id": "5ae30d02081e34940395696e",
    "balance": "3,324.41",
    "age": 49,
    "eyeColor": "brown",
    "name": {
      "first": "Liz",
      "last": "Harrell"
    },
    "company": "Codact",
    "email": "[email protected]",
    "phone": "+1 (957) 530-3553",
    "address": "575 Waldane Court, Sussex, New Hampshire, 9982",
    "registered": "2017-01-13T10:09:11 -00:00",
    "latitude": -86.073799,
    "longitude": -48.573135
  },
  {
    "_id": "5ae30d024a9ea40d4c2ca212",
    "balance": "2,364.05",
    "age": 28,
    "eyeColor": "green",
    "name": {
      "first": "Luz",
      "last": "Sharp"
    },
    "company": "Hopeli",
    "email": "[email protected]",
    "phone": "+1 (843) 536-2721",
    "address": "225 Huron Street, Richford, Iowa, 7066",
    "registered": "2016-09-16T08:01:17 -01:00",
    "latitude": 26.918633,
    "longitude": -24.304009
  },
  {
    "_id": "5ae30d02b08119b44fd33946",
    "balance": "2,040.30",
    "age": 21,
    "eyeColor": "blue",
    "name": {
      "first": "Celina",
      "last": "Hammond"
    },
    "company": "Digigene",
    "email": "[email protected]",
    "phone": "+1 (964) 445-3260",
    "address": "433 Sumner Place, Wedgewood, Arizona, 6033",
    "registered": "2017-11-21T05:27:38 -00:00",
    "latitude": -21.626178,
    "longitude": -65.326728
  },
  {
    "_id": "5ae30d021508ef1683fca66d",
    "balance": "1,930.37",
    "age": 50,
    "eyeColor": "green",
    "name": {
      "first": "Alexis",
      "last": "Chan"
    },
    "company": "Concility",
    "email": "[email protected]",
    "phone": "+1 (973) 527-2328",
    "address": "444 Anchorage Place, Dundee, Alaska, 2502",
    "registered": "2014-02-14T12:21:44 -00:00",
    "latitude": -2.539619,
    "longitude": 95.660992
  },
  {
    "_id": "5ae30d02fc383d361e1de77c",
    "balance": "1,887.65",
    "age": 35,
    "eyeColor": "grey",
    "name": {
      "first": "Jessica",
      "last": "Park"
    },
    "company": "Kangle",
    "email": "[email protected]",
    "phone": "+1 (842) 474-2417",
    "address": "183 Mersereau Court, Hollymead, Colorado, 8797",
    "registered": "2014-01-21T09:50:45 -00:00",
    "latitude": -55.563378,
    "longitude": -17.215224
  },
  {
    "_id": "5ae30d02aec3b3758323f307",
    "balance": "2,271.43",
    "age": 44,
    "eyeColor": "blue",
    "name": {
      "first": "Sutton",
      "last": "Nixon"
    },
    "company": "Frolix",
    "email": "[email protected]",
    "phone": "+1 (927) 475-2590",
    "address": "483 Junius Street, Corinne, Kentucky, 6142",
    "registered": "2016-02-16T07:51:36 -00:00",
    "latitude": -69.55902,
    "longitude": 49.509631
  },
  {
    "_id": "5ae30d0210036f470ba9906f",
    "balance": "2,268.91",
    "age": 46,
    "eyeColor": "green",
    "name": {
      "first": "Reba",
      "last": "Osborne"
    },
    "company": "Ovation",
    "email": "[email protected]",
    "phone": "+1 (905) 502-3225",
    "address": "586 Beaver Street, Edenburg, Wyoming, 8712",
    "registered": "2016-03-02T06:08:48 -00:00",
    "latitude": 57.737613,
    "longitude": -49.301794
  },
  {
    "_id": "5ae30d02f2a230afbc80f82e",
    "balance": "3,633.00",
    "age": 54,
    "eyeColor": "green",
    "name": {
      "first": "Hardy",
      "last": "Cotton"
    },
    "company": "Futurize",
    "email": "[email protected]",
    "phone": "+1 (969) 427-2003",
    "address": "923 Miami Court, Knowlton, Ohio, 4026",
    "registered": "2016-02-22T07:35:28 -00:00",
    "latitude": 84.25911,
    "longitude": 136.825862
  }
]

This is really a large file, so i pasted some of it. How can I find all the people with common surnames (that means common first name's of the json data)?

I'm using ajax request in javascript.

5
  • 2
    Possible duplicate of Javascript find json value Commented Feb 8, 2019 at 21:45
  • What is the expected output for the sample data you have given? Also, is it about surnames or first names? You mention both... Commented Feb 8, 2019 at 21:48
  • 1
    What do you mean by 'common' surnames? Just names or names that statisfy some criterias? Commented Feb 8, 2019 at 21:51
  • Hi @trincot, the json file has name object in it and there are multiple names which repeats again and i need to find those common names according to their surname, that means first name. Commented Feb 8, 2019 at 22:20
  • Ow, well in my understanding surname is last name, not first name, but OK, I now see you mean first name. Commented Feb 8, 2019 at 22:24

3 Answers 3

1

you can use reduce() and group the ids by name,

const data = [
  {
    "_id": "5ae30d020135436357a4732e",
    "balance": "1,171.56",
    "age": 46,
    "eyeColor": "hazel",
    "name": {
      "first": "Colon",
      "last": "Key"
    },
    "company": "Orbaxter",
    "email": "[email protected]",
    "phone": "+1 (938) 402-3422",
    "address": "706 Tennis Court, Kimmell, Indiana, 4057",
    "registered": "2014-12-24T10:31:42 -00:00",
    "latitude": 22.250647,
    "longitude": -67.325278
  },
  {
    "_id": "5ae30d027f167186f52e2e6c",
    "balance": "1,383.36",
    "age": 23,
    "eyeColor": "hazel",
    "name": {
      "first": "Colon",
      "last": "Walls"
    },
    "company": "Bizmatic",
    "email": "[email protected]",
    "phone": "+1 (817) 569-3406",
    "address": "187 Lewis Avenue, Lutsen, Oregon, 7387",
    "registered": "2017-06-19T09:24:42 -01:00",
    "latitude": -60.642235,
    "longitude": -62.947158
  },
  {
    "_id": "5ae30d023f1f434dcb1ea87d",
    "balance": "2,338.81",
    "age": 21,
    "eyeColor": "grey",
    "name": {
      "first": "Colon",
      "last": "Booker"
    },
    "company": "Aquazure",
    "email": "[email protected]",
    "phone": "+1 (861) 495-3729",
    "address": "633 Billings Place, Jacumba, Louisiana, 1488",
    "registered": "2017-12-11T08:53:27 -00:00",
    "latitude": 18.681413,
    "longitude": 93.058578
  },
  {
    "_id": "5ae30d02826a978c51641cfb",
    "balance": "2,959.74",
    "age": 40,
    "eyeColor": "hazel",
    "name": {
      "first": "Tammi",
      "last": "Salazar"
    },
    "company": "Terascape",
    "email": "[email protected]",
    "phone": "+1 (906) 405-2566",
    "address": "470 Elliott Walk, Gibbsville, New Mexico, 7298",
    "registered": "2014-08-06T09:06:17 -01:00",
    "latitude": -75.890626,
    "longitude": 19.674008
  },
  {
    "_id": "5ae30d02dd1046807a0e3636",
    "balance": "1,960.08",
    "age": 42,
    "eyeColor": "hazel",
    "name": {
      "first": "Tammi",
      "last": "Merritt"
    },
    "company": "Digial",
    "email": "[email protected]",
    "phone": "+1 (802) 566-3481",
    "address": "189 Jerome Street, Hoehne, Palau, 8982",
    "registered": "2017-06-18T10:16:48 -01:00",
    "latitude": -16.254906,
    "longitude": 112.500547
  },
  {
    "_id": "5ae30d02081e34940395696e",
    "balance": "3,324.41",
    "age": 49,
    "eyeColor": "brown",
    "name": {
      "first": "Liz",
      "last": "Harrell"
    },
    "company": "Codact",
    "email": "[email protected]",
    "phone": "+1 (957) 530-3553",
    "address": "575 Waldane Court, Sussex, New Hampshire, 9982",
    "registered": "2017-01-13T10:09:11 -00:00",
    "latitude": -86.073799,
    "longitude": -48.573135
  },
  {
    "_id": "5ae30d024a9ea40d4c2ca212",
    "balance": "2,364.05",
    "age": 28,
    "eyeColor": "green",
    "name": {
      "first": "Luz",
      "last": "Sharp"
    },
    "company": "Hopeli",
    "email": "[email protected]",
    "phone": "+1 (843) 536-2721",
    "address": "225 Huron Street, Richford, Iowa, 7066",
    "registered": "2016-09-16T08:01:17 -01:00",
    "latitude": 26.918633,
    "longitude": -24.304009
  },
  {
    "_id": "5ae30d02b08119b44fd33946",
    "balance": "2,040.30",
    "age": 21,
    "eyeColor": "blue",
    "name": {
      "first": "Luz",
      "last": "Hammond"
    },
    "company": "Digigene",
    "email": "[email protected]",
    "phone": "+1 (964) 445-3260",
    "address": "433 Sumner Place, Wedgewood, Arizona, 6033",
    "registered": "2017-11-21T05:27:38 -00:00",
    "latitude": -21.626178,
    "longitude": -65.326728
  },
  {
    "_id": "5ae30d021508ef1683fca66d",
    "balance": "1,930.37",
    "age": 50,
    "eyeColor": "green",
    "name": {
      "first": "Luz",
      "last": "Chan"
    },
    "company": "Concility",
    "email": "[email protected]",
    "phone": "+1 (973) 527-2328",
    "address": "444 Anchorage Place, Dundee, Alaska, 2502",
    "registered": "2014-02-14T12:21:44 -00:00",
    "latitude": -2.539619,
    "longitude": 95.660992
  },
  {
    "_id": "5ae30d02fc383d361e1de77c",
    "balance": "1,887.65",
    "age": 35,
    "eyeColor": "grey",
    "name": {
      "first": "Luz",
      "last": "Park"
    },
    "company": "Kangle",
    "email": "[email protected]",
    "phone": "+1 (842) 474-2417",
    "address": "183 Mersereau Court, Hollymead, Colorado, 8797",
    "registered": "2014-01-21T09:50:45 -00:00",
    "latitude": -55.563378,
    "longitude": -17.215224
  },
  {
    "_id": "5ae30d02aec3b3758323f307",
    "balance": "2,271.43",
    "age": 44,
    "eyeColor": "blue",
    "name": {
      "first": "Sutton",
      "last": "Nixon"
    },
    "company": "Frolix",
    "email": "[email protected]",
    "phone": "+1 (927) 475-2590",
    "address": "483 Junius Street, Corinne, Kentucky, 6142",
    "registered": "2016-02-16T07:51:36 -00:00",
    "latitude": -69.55902,
    "longitude": 49.509631
  },
  {
    "_id": "5ae30d0210036f470ba9906f",
    "balance": "2,268.91",
    "age": 46,
    "eyeColor": "green",
    "name": {
      "first": "Sutton",
      "last": "Osborne"
    },
    "company": "Ovation",
    "email": "[email protected]",
    "phone": "+1 (905) 502-3225",
    "address": "586 Beaver Street, Edenburg, Wyoming, 8712",
    "registered": "2016-03-02T06:08:48 -00:00",
    "latitude": 57.737613,
    "longitude": -49.301794
  },
  {
    "_id": "5ae30d02f2a230afbc80f82e",
    "balance": "3,633.00",
    "age": 54,
    "eyeColor": "green",
    "name": {
      "first": "Hardy",
      "last": "Cotton"
    },
    "company": "Futurize",
    "email": "[email protected]",
    "phone": "+1 (969) 427-2003",
    "address": "923 Miami Court, Knowlton, Ohio, 4026",
    "registered": "2016-02-22T07:35:28 -00:00",
    "latitude": 84.25911,
    "longitude": 136.825862
  },
  {
    "_id": "5ae30d02519a87f76fc8a0e6",
    "balance": "1,902.75",
    "age": 47,
    "eyeColor": "green",
    "name": {
      "first": "Ashlee",
      "last": "Gregory"
    }
  }
];

const result = data.reduce((all, curr) => {
  (all[curr.name.first] = all[curr.name.first] || []).push(
  curr["_id"]
  );

  return all;
}, {});

const formattedResult = Object.entries(result).map(([name, ids]) => ({
  name,
  ids,
  repeated : ids.length,  
}));

console.log(formattedResult);

// for top 5 repeated names : 
const topFive = formattedResult.sort((a,b) => b.repeated - a.repeated).slice(0, 5);

the resulting Object will have the name as keys and the ids of the corresponding values with the same name, ( i modified your input for the second name for the sake of the demo )

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the answer man! But i'm looking for common names(first name) which are repeated more in increasing order. also wants to find individual lengths of each common names.
i updated the answer so the result would be an array of objects containing the name, the ids of users having that name and repeated times, they are in the same order of the original list,
It works!! I tried this with my large json file and there are around 20,000 names in it and i also tried top 5 repeated common names but im not able to get it cause each name repeats 5 or 6 times equally .
cool, but i don't understand what is it you're not able to get
you are finding it by the id's repeated : ids.length + " times" but i want them by the first name. in my original json file 'Colon' name repeated 50 times but in your code its only calculating 5 times.
|
1

If you want to gather the names, you can map on your data and extract the name and then first:

const names = data.map(item => item.name ? item.name.first : '');

If you want to group all people with the same name, then use reduce. For example to gather the _id's:

const sameNames = data.reduce((accum, { name: { first }, _id }) => {
  accum[first] = [...(accum[first] || []), _id];
  return accum;
}, {});

Here is an example:

const data = [
  {
    "_id": "5ae30d020135436357a4732e",
    "balance": "1,171.56",
    "age": 46,
    "eyeColor": "hazel",
    "name": {
      "first": "Colon",
      "last": "Key"
    },
    "company": "Orbaxter",
    "email": "[email protected]",
    "phone": "+1 (938) 402-3422",
    "address": "706 Tennis Court, Kimmell, Indiana, 4057",
    "registered": "2014-12-24T10:31:42 -00:00",
    "latitude": 22.250647,
    "longitude": -67.325278
  },
  {
    "_id": "5ae30d027f167186f52e2e6c",
    "balance": "1,383.36",
    "age": 23,
    "eyeColor": "hazel",
    "name": {
      "first": "Byrd",
      "last": "Walls"
    },
    "company": "Bizmatic",
    "email": "[email protected]",
    "phone": "+1 (817) 569-3406",
    "address": "187 Lewis Avenue, Lutsen, Oregon, 7387",
    "registered": "2017-06-19T09:24:42 -01:00",
    "latitude": -60.642235,
    "longitude": -62.947158
  },
  {
    "_id": "5ae30d023f1f434dcb1ea87d",
    "balance": "2,338.81",
    "age": 21,
    "eyeColor": "grey",
    "name": {
      "first": "Colon",
      "last": "Booker"
    },
    "company": "Aquazure",
    "email": "[email protected]",
    "phone": "+1 (861) 495-3729",
    "address": "633 Billings Place, Jacumba, Louisiana, 1488",
    "registered": "2017-12-11T08:53:27 -00:00",
    "latitude": 18.681413,
    "longitude": 93.058578
  },
  {
    "_id": "5ae30d02826a978c51641cfb",
    "balance": "2,959.74",
    "age": 40,
    "eyeColor": "hazel",
    "name": {
      "first": "Tammi",
      "last": "Salazar"
    },
    "company": "Terascape",
    "email": "[email protected]",
    "phone": "+1 (906) 405-2566",
    "address": "470 Elliott Walk, Gibbsville, New Mexico, 7298",
    "registered": "2014-08-06T09:06:17 -01:00",
    "latitude": -75.890626,
    "longitude": 19.674008
  },
  {
    "_id": "5ae30d02dd1046807a0e3636",
    "balance": "1,960.08",
    "age": 42,
    "eyeColor": "hazel",
    "name": {
      "first": "Maryellen",
      "last": "Merritt"
    },
    "company": "Digial",
    "email": "[email protected]",
    "phone": "+1 (802) 566-3481",
    "address": "189 Jerome Street, Hoehne, Palau, 8982",
    "registered": "2017-06-18T10:16:48 -01:00",
    "latitude": -16.254906,
    "longitude": 112.500547
  },
  {
    "_id": "5ae30d02081e34940395696e",
    "balance": "3,324.41",
    "age": 49,
    "eyeColor": "brown",
    "name": {
      "first": "Liz",
      "last": "Harrell"
    },
    "company": "Codact",
    "email": "[email protected]",
    "phone": "+1 (957) 530-3553",
    "address": "575 Waldane Court, Sussex, New Hampshire, 9982",
    "registered": "2017-01-13T10:09:11 -00:00",
    "latitude": -86.073799,
    "longitude": -48.573135
  },
  {
    "_id": "5ae30d024a9ea40d4c2ca212",
    "balance": "2,364.05",
    "age": 28,
    "eyeColor": "green",
    "name": {
      "first": "Luz",
      "last": "Sharp"
    },
    "company": "Hopeli",
    "email": "[email protected]",
    "phone": "+1 (843) 536-2721",
    "address": "225 Huron Street, Richford, Iowa, 7066",
    "registered": "2016-09-16T08:01:17 -01:00",
    "latitude": 26.918633,
    "longitude": -24.304009
  },
  {
    "_id": "5ae30d02b08119b44fd33946",
    "balance": "2,040.30",
    "age": 21,
    "eyeColor": "blue",
    "name": {
      "first": "Liz",
      "last": "Hammond"
    },
    "company": "Digigene",
    "email": "[email protected]",
    "phone": "+1 (964) 445-3260",
    "address": "433 Sumner Place, Wedgewood, Arizona, 6033",
    "registered": "2017-11-21T05:27:38 -00:00",
    "latitude": -21.626178,
    "longitude": -65.326728
  },
  {
    "_id": "5ae30d021508ef1683fca66d",
    "balance": "1,930.37",
    "age": 50,
    "eyeColor": "green",
    "name": {
      "first": "Alexis",
      "last": "Chan"
    },
    "company": "Concility",
    "email": "[email protected]",
    "phone": "+1 (973) 527-2328",
    "address": "444 Anchorage Place, Dundee, Alaska, 2502",
    "registered": "2014-02-14T12:21:44 -00:00",
    "latitude": -2.539619,
    "longitude": 95.660992
  },
  {
    "_id": "5ae30d02fc383d361e1de77c",
    "balance": "1,887.65",
    "age": 35,
    "eyeColor": "grey",
    "name": {
      "first": "Jessica",
      "last": "Park"
    },
    "company": "Kangle",
    "email": "[email protected]",
    "phone": "+1 (842) 474-2417",
    "address": "183 Mersereau Court, Hollymead, Colorado, 8797",
    "registered": "2014-01-21T09:50:45 -00:00",
    "latitude": -55.563378,
    "longitude": -17.215224
  },
  {
    "_id": "5ae30d02aec3b3758323f307",
    "balance": "2,271.43",
    "age": 44,
    "eyeColor": "blue",
    "name": {
      "first": "Sutton",
      "last": "Nixon"
    },
    "company": "Frolix",
    "email": "[email protected]",
    "phone": "+1 (927) 475-2590",
    "address": "483 Junius Street, Corinne, Kentucky, 6142",
    "registered": "2016-02-16T07:51:36 -00:00",
    "latitude": -69.55902,
    "longitude": 49.509631
  },
  {
    "_id": "5ae30d0210036f470ba9906f",
    "balance": "2,268.91",
    "age": 46,
    "eyeColor": "green",
    "name": {
      "first": "Reba",
      "last": "Osborne"
    },
    "company": "Ovation",
    "email": "[email protected]",
    "phone": "+1 (905) 502-3225",
    "address": "586 Beaver Street, Edenburg, Wyoming, 8712",
    "registered": "2016-03-02T06:08:48 -00:00",
    "latitude": 57.737613,
    "longitude": -49.301794
  },
  {
    "_id": "5ae30d02f2a230afbc80f82e",
    "balance": "3,633.00",
    "age": 54,
    "eyeColor": "green",
    "name": {
      "first": "Sutton",
      "last": "Cotton"
    },
    "company": "Futurize",
    "email": "[email protected]",
    "phone": "+1 (969) 427-2003",
    "address": "923 Miami Court, Knowlton, Ohio, 4026",
    "registered": "2016-02-22T07:35:28 -00:00",
    "latitude": 84.25911,
    "longitude": 136.825862
  },
  {
    "_id": "5ae30d02519a87f76fc8a0e6",
    "balance": "1,902.75",
    "age": 47,
    "eyeColor": "green",
    "name": {
      "first": "Ashlee",
      "last": "Gregory"
    }
  }
];

const sameNames = data.reduce((accum, { name: { first }, _id }) => {
  accum[first] = [...(accum[first] || []), _id];
  return accum;
}, {});

console.log(sameNames);

2 Comments

This does not group the users by first name though. It only return an array of first names
You are right, the OP updated its question, I will adjust my answer
0

To group your users by first name you can use the reduce function. This will create an object which has first names as keys and user groups as values.

  • If the first name is not in the final object, you add it as a key with a an array containing only that user as a value
  • If it's already there, append it to the value

const groupUsersByFirstName = arr => arr.reduce((acc, curr) => {
  if (!(curr.name.first in acc)) {
     acc[curr.name.first] = [curr];
  } else {
     acc[curr.name.first].append(curr);
  }
  
  return acc;
}, {});

const users = [{
    "_id": "5ae30d020135436357a4732e",
    "balance": "1,171.56",
    "age": 46,
    "eyeColor": "hazel",
    "name": {
      "first": "Colon",
      "last": "Key"
    },
    "company": "Orbaxter",
    "email": "[email protected]",
    "phone": "+1 (938) 402-3422",
    "address": "706 Tennis Court, Kimmell, Indiana, 4057",
    "registered": "2014-12-24T10:31:42 -00:00",
    "latitude": 22.250647,
    "longitude": -67.325278
  },
  {
    "_id": "5ae30d027f167186f52e2e6c",
    "balance": "1,383.36",
    "age": 23,
    "eyeColor": "hazel",
    "name": {
      "first": "Byrd",
      "last": "Walls"
    },
    "company": "Bizmatic",
    "email": "[email protected]",
    "phone": "+1 (817) 569-3406",
    "address": "187 Lewis Avenue, Lutsen, Oregon, 7387",
    "registered": "2017-06-19T09:24:42 -01:00",
    "latitude": -60.642235,
    "longitude": -62.947158
  }, {
    "_id": "5ae30d023f1f434dcb1ea87d",
    "balance": "2,338.81",
    "age": 21,
    "eyeColor": "grey",
    "name": {
      "first": "Sondra",
      "last": "Booker"
    },
    "company": "Aquazure",
    "email": "[email protected]",
    "phone": "+1 (861) 495-3729",
    "address": "633 Billings Place, Jacumba, Louisiana, 1488",
    "registered": "2017-12-11T08:53:27 -00:00",
    "latitude": 18.681413,
    "longitude": 93.058578
  }, {
    "_id": "5ae30d02826a978c51641cfb",
    "balance": "2,959.74",
    "age": 40,
    "eyeColor": "hazel",
    "name": {
      "first": "Tammi",
      "last": "Salazar"
    },
    "company": "Terascape",
    "email": "[email protected]",
    "phone": "+1 (906) 405-2566",
    "address": "470 Elliott Walk, Gibbsville, New Mexico, 7298",
    "registered": "2014-08-06T09:06:17 -01:00",
    "latitude": -75.890626,
    "longitude": 19.674008
  }, {
    "_id": "5ae30d02dd1046807a0e3636",
    "balance": "1,960.08",
    "age": 42,
    "eyeColor": "hazel",
    "name": {
      "first": "Maryellen",
      "last": "Merritt"
    },
    "company": "Digial",
    "email": "[email protected]",
    "phone": "+1 (802) 566-3481",
    "address": "189 Jerome Street, Hoehne, Palau, 8982",
    "registered": "2017-06-18T10:16:48 -01:00",
    "latitude": -16.254906,
    "longitude": 112.500547
  }, {
    "_id": "5ae30d02081e34940395696e",
    "balance": "3,324.41",
    "age": 49,
    "eyeColor": "brown",
    "name": {
      "first": "Liz",
      "last": "Harrell"
    },
    "company": "Codact",
    "email": "[email protected]",
    "phone": "+1 (957) 530-3553",
    "address": "575 Waldane Court, Sussex, New Hampshire, 9982",
    "registered": "2017-01-13T10:09:11 -00:00",
    "latitude": -86.073799,
    "longitude": -48.573135
  }, {
    "_id": "5ae30d024a9ea40d4c2ca212",
    "balance": "2,364.05",
    "age": 28,
    "eyeColor": "green",
    "name": {
      "first": "Luz",
      "last": "Sharp"
    },
    "company": "Hopeli",
    "email": "[email protected]",
    "phone": "+1 (843) 536-2721",
    "address": "225 Huron Street, Richford, Iowa, 7066",
    "registered": "2016-09-16T08:01:17 -01:00",
    "latitude": 26.918633,
    "longitude": -24.304009
  }, {
    "_id": "5ae30d02b08119b44fd33946",
    "balance": "2,040.30",
    "age": 21,
    "eyeColor": "blue",
    "name": {
      "first": "Celina",
      "last": "Hammond"
    },
    "company": "Digigene",
    "email": "[email protected]",
    "phone": "+1 (964) 445-3260",
    "address": "433 Sumner Place, Wedgewood, Arizona, 6033",
    "registered": "2017-11-21T05:27:38 -00:00",
    "latitude": -21.626178,
    "longitude": -65.326728
  }, {
    "_id": "5ae30d021508ef1683fca66d",
    "balance": "1,930.37",
    "age": 50,
    "eyeColor": "green",
    "name": {
      "first": "Alexis",
      "last": "Chan"
    },
    "company": "Concility",
    "email": "[email protected]",
    "phone": "+1 (973) 527-2328",
    "address": "444 Anchorage Place, Dundee, Alaska, 2502",
    "registered": "2014-02-14T12:21:44 -00:00",
    "latitude": -2.539619,
    "longitude": 95.660992
  }, {
    "_id": "5ae30d02fc383d361e1de77c",
    "balance": "1,887.65",
    "age": 35,
    "eyeColor": "grey",
    "name": {
      "first": "Jessica",
      "last": "Park"
    },
    "company": "Kangle",
    "email": "[email protected]",
    "phone": "+1 (842) 474-2417",
    "address": "183 Mersereau Court, Hollymead, Colorado, 8797",
    "registered": "2014-01-21T09:50:45 -00:00",
    "latitude": -55.563378,
    "longitude": -17.215224
  }, {
    "_id": "5ae30d02aec3b3758323f307",
    "balance": "2,271.43",
    "age": 44,
    "eyeColor": "blue",
    "name": {
      "first": "Sutton",
      "last": "Nixon"
    },
    "company": "Frolix",
    "email": "[email protected]",
    "phone": "+1 (927) 475-2590",
    "address": "483 Junius Street, Corinne, Kentucky, 6142",
    "registered": "2016-02-16T07:51:36 -00:00",
    "latitude": -69.55902,
    "longitude": 49.509631
  }, {
    "_id": "5ae30d0210036f470ba9906f",
    "balance": "2,268.91",
    "age": 46,
    "eyeColor": "green",
    "name": {
      "first": "Reba",
      "last": "Osborne"
    },
    "company": "Ovation",
    "email": "[email protected]",
    "phone": "+1 (905) 502-3225",
    "address": "586 Beaver Street, Edenburg, Wyoming, 8712",
    "registered": "2016-03-02T06:08:48 -00:00",
    "latitude": 57.737613,
    "longitude": -49.301794
  }, {
    "_id": "5ae30d02f2a230afbc80f82e",
    "balance": "3,633.00",
    "age": 54,
    "eyeColor": "green",
    "name": {
      "first": "Hardy",
      "last": "Cotton"
    },
    "company": "Futurize",
    "email": "[email protected]",
    "phone": "+1 (969) 427-2003",
    "address": "923 Miami Court, Knowlton, Ohio, 4026",
    "registered": "2016-02-22T07:35:28 -00:00",
    "latitude": 84.25911,
    "longitude": 136.825862
  }, {
    "_id": "5ae30d02519a87f76fc8a0e6",
    "balance": "1,902.75",
    "age": 47,
    "eyeColor": "green",
    "name": {
      "first": "Ashlee",
      "last": "Gregory"
    }
  }
];

const grouped = groupUsersByFirstName(users);

console.log('first names', Object.keys(grouped));
console.log('grouped users', grouped);

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.