- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找 json 逻辑 2 小时,但没有找到任何解决方案。我的 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 请求。
最佳答案
您可以使用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);
生成的对象
将以name
作为键,并以相同名称的相应值的ids
(我修改了您的输入为了演示而使用第二个名称)
关于javascript - 如何使用 javascript 查找 json 中的通用名称对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54600637/
最近开始学习MongoDB。今天老师教了我们 mongoexport 命令。在练习时,我遇到了一个典型的问题,包括教练在内的其他同学都没有遇到过。我在我的 Windows 10 机器上使用 Mongo
我是 JSON Schema 的新手,读过什么是 JSON Schema 等等。但我不知道如何将 JSON Schema 链接到 JSON 以针对该 JSON Schema 进行验证。谁能解释一下?
在 xml 中,我可以在另一个 xml 文件中包含一个文件并使用它。如果您的软件从 xml 获取配置文件但没有任何方法来分离配置,如 apache/ngnix(nginx.conf - site-av
我有一个 JSON 对象,其中包含一个本身是 JSON 对象的字符串。我如何反序列化它? 我希望能够做类似的事情: #[derive(Deserialize)] struct B { c: S
考虑以下 JSON { "a": "{\"b\": 12, \"c\": \"test\"}" } 我想定义一个泛型读取 Reads[Outer[T]]对于这种序列化的 Json import
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 11 个月前关闭。 Improve
我的旧项目在 MySQL 中有 Standard JSON 格式的数据。 对于我在 JS (Node.js) 和 DynamoDB 中的全新项目,关于 Standard JSON格式: 是否建议将其转
JSON 值字符串、数字、true、false、null 是否是有效的 JSON? 即,是 true 一个有效的 JSON 文档?还是必须是数组/对象? 一些验证器接受这个(例如 http://jso
我有一个 JSON 字符串,其中一个字段是文本字段。这个文本字段可以包含用户在 UI 中输入的文本,如果他们输入的文本是 JSON 文本,也许是为了说明一些编码,我需要对他们的文本进行编码,以便它不会
我正在通过 IBM MQ 调用处理数据,当由 ColdFusion 10 (10,0,11,285437) 序列化时,0 将作为 +0.0 返回,它会导致无效的 JSON并且无法反序列化。 stPol
我正在从三个数组中生成一个散列,然后尝试构建一个 json。我通过 json object has array 成功了。 require 'json' A = [['A1', 'A2', 'A3'],
我从 API 接收 JSON,响应可以是 30 种类型之一。每种类型都有一组唯一的字段,但所有响应都有一个字段 type 说明它是哪种类型。 我的方法是使用serde .我为每种响应类型创建一个结构并
我正在下载一个 JSON 文件,我已将其检查为带有“https://jsonlint.com”的有效 JSON 到文档目录。然后我打开文件并再次检查,结果显示为无效的 JSON。这怎么可能????这是
我正在尝试根据从 API 接收到的数据动态创建一个 JSON 对象。 收到的示例数据:将数据解码到下面给出的 CiItems 结构中 { "class_name": "test", "
我想从字符串转换为对象。 来自 {"key1": "{\n \"key2\": \"value2\",\n \"key3\": {\n \"key4\": \"value4\"\n }\n
目前我正在使用以下代码将嵌套的 json 转换为扁平化的 json: import ( "fmt" "github.com/nytlabs/gojsonexplode" ) func
我有一个使用来自第三方 API 的数据的应用程序。我需要将 json 解码为一个结构,这需要该结构具有“传入”json 字段的 json 标签。传出的 json 字段具有不同的命名约定,因此我需要不同
我想使用 JSON 架构来验证某些值。我有两个对象,称它们为 trackedItems 和 trackedItemGroups。 trackedItemGroups 是组名称和 trackedItem
考虑以下案例类模式, case class Y (a: String, b: String) case class X (dummy: String, b: Y) 字段b是可选的,我的一些数据集没有字
我正在存储 cat ~/path/to/file/blah | 的输出jq tojson 在一个变量中,稍后在带有 JSON 内容的 curl POST 中使用。它运作良好,但它删除了所有换行符。我知
我是一名优秀的程序员,十分优秀!