【问题标题】:how to find common name objects in json using javascript如何使用javascript在json中查找通用名称对象
【发布时间】:2019-07-03 04:20:57
【问题描述】:

我正在寻找 2 小时的 json 逻辑,但我没有找到任何解决方案。 我的 json 数据如下所示:

[
  {
    "_id": "5ae30d020135436357a4732e",
    "balance": "1,171.56",
    "age": 46,
    "eyeColor": "hazel",
    "name": {
      "first": "Colon",
      "last": "Key"
    },
    "company": "Orbaxter",
    "email": "colonkey@orbaxter.com",
    "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": "byrdwalls@bizmatic.com",
    "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": "sondrabooker@aquazure.com",
    "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": "tammisalazar@terascape.com",
    "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": "maryellenmerritt@digial.com",
    "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": "lizharrell@codact.com",
    "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": "luzsharp@hopeli.com",
    "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": "celinahammond@digigene.com",
    "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": "alexischan@concility.com",
    "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": "jessicapark@kangle.com",
    "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": "suttonnixon@frolix.com",
    "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": "rebaosborne@ovation.com",
    "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": "hardycotton@futurize.com",
    "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
  }
]

这真的是一个大文件,所以我粘贴了一些。我怎样才能找到所有具有共同姓氏的人(这意味着 json 数据的共同名字)?

我在 javascript 中使用 ajax 请求。

【问题讨论】:

  • 您给出的样本数据的预期输出是什么?另外,是关于姓氏还是名字?你提到两个......
  • “普通”姓氏是什么意思?只是名称或满足某些条件的名称?
  • 嗨@trincot,json文件中有名称对象,并且有多个名称再次重复,我需要根据他们的姓氏找到这些常用名称,这意味着名字。
  • 哦,好吧,在我的理解中 surname 是姓氏,而不是名字,但是好的,我现在明白你的意思是名字。

标签: javascript json


【解决方案1】:

您可以使用reduce() 并按名称对 id 进行分组,

const data = [
  {
    "_id": "5ae30d020135436357a4732e",
    "balance": "1,171.56",
    "age": 46,
    "eyeColor": "hazel",
    "name": {
      "first": "Colon",
      "last": "Key"
    },
    "company": "Orbaxter",
    "email": "colonkey@orbaxter.com",
    "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": "byrdwalls@bizmatic.com",
    "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": "sondrabooker@aquazure.com",
    "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": "tammisalazar@terascape.com",
    "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": "maryellenmerritt@digial.com",
    "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": "lizharrell@codact.com",
    "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": "luzsharp@hopeli.com",
    "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": "celinahammond@digigene.com",
    "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": "alexischan@concility.com",
    "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": "jessicapark@kangle.com",
    "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": "suttonnixon@frolix.com",
    "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": "rebaosborne@ovation.com",
    "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": "hardycotton@futurize.com",
    "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);

生成的Object 将具有name 作为键和具有相同名称的对应值的ids,(为了演示,我将您的输入修改为第二个名称)

【讨论】:

  • 感谢您的回答!但我正在寻找以递增顺序重复更多的常用名称(名字)。还想找到每个常用名称的单独长度。
  • 我更新了答案,因此结果将是一个包含name的对象数组,具有该名称的用户ID和repeated时间,它们与原始列表的顺序相同,
  • 有效!!我用我的大 json 文件尝试了这个,其中大约有 20,000 个名称,我还尝试了前 5 个重复的常用名称,但我无法得到它,因为每个名称都重复 5 或 6 次。
  • 酷,但我不明白你无法获得什么
  • 您通过 id 的重复找到它:ids.length + "times" 但我想要他们的名字。在我原来的 json 文件中,“冒号”名称重复了 50 次,但在您的代码中,它只计算了 5 次。
【解决方案2】:

如果您想收集名称,可以映射到您的数据并提取name,然后提取first

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

如果要将所有同名的人分组,请使用reduce。例如收集_id的:

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

这是一个例子:

const data = [
  {
    "_id": "5ae30d020135436357a4732e",
    "balance": "1,171.56",
    "age": 46,
    "eyeColor": "hazel",
    "name": {
      "first": "Colon",
      "last": "Key"
    },
    "company": "Orbaxter",
    "email": "colonkey@orbaxter.com",
    "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": "byrdwalls@bizmatic.com",
    "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": "sondrabooker@aquazure.com",
    "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": "tammisalazar@terascape.com",
    "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": "maryellenmerritt@digial.com",
    "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": "lizharrell@codact.com",
    "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": "luzsharp@hopeli.com",
    "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": "celinahammond@digigene.com",
    "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": "alexischan@concility.com",
    "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": "jessicapark@kangle.com",
    "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": "suttonnixon@frolix.com",
    "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": "rebaosborne@ovation.com",
    "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": "hardycotton@futurize.com",
    "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);

【讨论】:

  • 这不会按名字对用户进行分组。它只返回一个名字数组
  • 你是对的,OP更新了它的问题,我会调整我的答案
【解决方案3】:

要按名字对您的用户进行分组,您可以使用reduce 函数。这将创建一个对象,其中 first names 作为 keysuser groups 作为 values

  • 如果 名字 不在最终对象中,则将其作为键添加到一个数组中,该数组仅包含该用户作为值
  • 如果已经存在,请将其附加到值

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": "colonkey@orbaxter.com",
    "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": "byrdwalls@bizmatic.com",
    "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": "sondrabooker@aquazure.com",
    "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": "tammisalazar@terascape.com",
    "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": "maryellenmerritt@digial.com",
    "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": "lizharrell@codact.com",
    "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": "luzsharp@hopeli.com",
    "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": "celinahammond@digigene.com",
    "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": "alexischan@concility.com",
    "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": "jessicapark@kangle.com",
    "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": "suttonnixon@frolix.com",
    "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": "rebaosborne@ovation.com",
    "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": "hardycotton@futurize.com",
    "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);

【讨论】:

    猜你喜欢
    • 2022-08-03
    • 1970-01-01
    • 2020-07-29
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多