使用lodash转换JSON响应

时间:2015-11-26 13:37:13

标签: lodash

我是lodash(v3.10.1)中的新手,并且很难理解。 希望有人可以提供帮助。

我输入的内容如下:

{
     {"id":1,"name":"Matthew","company":{"id":1,"name":"abc","industry":{"id":5,"name":"Medical"}}},
     {"id":2,"name":"Mark","company":{"id":1,"name":"abc","industry":{"id":5,"name":"Medical"}}},
     {"id":3,"name":"Luke","company":{"id":1,"name":"abc","industry":{"id":5,"name":"Medical"}}},
     {"id":4,"name":"John","company":{"id":1,"name":"abc","industry":{"id":5,"name":"Medical"}}},
     {"id":5,"name":"Paul","company":{"id":1,"name":"abc","industry":{"id":5,"name":"Medical"}}}
];

我想输出这个或接近这个:

{
     "industries": [
        {
            "industry":{
                "id":5,
                "name":"Medical",
                "companies": [
                    {
                        "company":{
                            "id":1,
                            "name":"abc",
                            "employees": [
                                {"id":1,"name":"Matthew"},
                                {"id":2,"name":"Mark"},
                                {"id":3,"name":"Luke"},
                                {"id":4,"name":"John"},
                                {"id":5,"name":"Paul"}
                            ]
                        }
                    }
                ]
            }
        }
    ]
}

2 个答案:

答案 0 :(得分:2)

这是让你接近你想要的东西。我将输出结构化为对象而不是数组。您的示例输出中不需要industriesindustry属性。输出结构如下所示:

{
  "industry name": {
    "id": "id of industry",
    "companies": [
      {
        "company name": "name of company",
        "id": "id of company",
        "employees": [
          {
            "id": "id of company",
            "name": "name of employee"
          }
        ]
      }
    ]
  }
}

我使用_.chain函数用lodash包装器对象包装集合。这使我能够明确地链接lodash函数。

从那里,我使用_.groupBy函数按行业名称对集合的元素进行分组。因为我正在链接,所以我不必再将数组传递给函数。它通过lodash包装器隐式传递。 _.groupBy的第二个参数是我想要将元素分组的值的路径。在这种情况下,它是行业名称的路径:company.industry.name_.groupBy返回一个对象,每个员工按行业分组(行业是此对象的关键字)。

然后我使用_.transform来转换每个行业对象。 _.transform基本上是_.reduce,但_.transform函数返回的结果始终是对象。

传递给_.transform函数的函数将针对对象中的每个键/值对执行。在该功能中,我再次使用_.groupBy按公司对员工进行分组。根据{{​​1}}的结果,我将值映射到我想要的每个员工对象的最终结构。

然后我调用_.groupBy函数,因为我想从lodash包装器对象中解包输出集合。

我希望这是有道理的。如果没有,我强烈建议您阅读Lo-Dash Essentials。读完这本书之后,我终于明白了为什么lodash如此有用。

_.value

您的示例的结果如下:

"use strict";

var _ = require('lodash');

var emps = [
  { "id": 1, "name": "Matthew", "company": { "id": 1, "name": "abc", "industry": { "id": 5, "name": "Medical" } } },
  { "id": 2, "name": "Mark", "company": { "id": 1, "name": "abc", "industry": { "id": 5, "name": "Medical" } } },
  { "id": 3, "name": "Luke", "company": { "id": 1, "name": "abc", "industry": { "id": 5, "name": "Medical" } } },
  { "id": 4, "name": "John", "company": { "id": 1, "name": "abc", "industry": { "id": 5, "name": "Medical" } } },
  { "id": 5, "name": "Paul", "company": { "id": 1, "name": "abc", "industry": { "id": 5, "name": "Medical" } } }
];

var result = _.chain(emps)
  .groupBy("company.industry.name")
  .transform(function(result, employees, industry) {
    result[industry] = {};
    result[industry].id = _.get(employees[0], "company.industry.id");

    result[ industry ][ 'companies' ] = _.map(_.groupBy(employees, "company.name"), function( employees, company ) {
      return {
        company: company,
        id: _.get(employees[ 0 ], 'company.id'),
        employees: _.map(employees, _.partialRight(_.pick, [ 'id', 'name' ]))
      };
    });
    return result;
  })
  .value();

答案 1 :(得分:1)

如果您想要与问题中的结构完全相同的结构,我可以使用jsonata库解决它:

(
    /* lets flatten it out for ease of accessing the properties*/
    $step1 :=    $ ~> | $ | 
            {
                "employee_id": id,
                "employee_name": name,
                "company_id": company.id,
                "company_name": company.name,
                "industry_id": company.industry.id,
                "industry_name": company.industry.name        
            }, 
            ["company", "id", "name"] |;
    
    /* now the magic begins*/
    $step2 := {
        "industries": 
            [($step1{                
                "industry" & $string(industry_id): ${
                    "id": $distinct(industry_id)@$I,
                    "name": $distinct(industry_name),
                    "companies": [({
                        "company" & $string(company_id): {
                            "id": $distinct(company_id),
                            "name": $distinct(company_name),
                            "employees": [$.{                                
                                    "id": $distinct(employee_id),
                                    "name": $distinct(employee_name)
                                
                            }]
                        }
                    } ~> $each(function($v){ {"company": $v} }))]
                }
                
            } ~> $each(function($v){ {"industry": $v} }))]
        
    };    
)

您可以在现场演示站点上看到它的运行情况:https://try.jsonata.org/VvW4uTRz_