猿问

使用 lodash 迭代内部对象

这是我的 JSON 对象:


{

"id": 2,

"full_name": "Karan Kumar",

"user_skills": [

   {

      "skill_id": 1,

      "skill": {

          "skill_name": "Angular 8"

       }

   },

   {

      "skill_id": 3,

      "skill": {

          "skill_name": "Java Spring"

       }

    }

],

"resource_allocateds": [

  {

     "project_id": 1,

     "end_date": "2019-06-20",

     "project": {

         "project_name": "Inventory System"

      }

  },

  {

      "project_id": 2,

      "end_date": "2020-01-15",

      "project": {

           "project_name": "Hospital Management System"

       }

    }

  ]

}

我想从上面的 JSON 中选择某些属性,并且我希望它是这样的:


{

    "id": 1,

    "full_name": "Karan Kumar",

    "skills": [{

        "skill_id": 1,

        "skill_name": "Angular 8"

    },

    {

        "skill_id": 3,

       "skill_name": "Java Spring"

    }],

    "resource_allocateds": [{

        "project_id": 1,

        "end_date": "2019-06-20",

        "project_name": "Inventory System"

    },

    {

        "project_id": 2,

        "end_date": "2020-01-15",

        "project_name": "Hospital Management System"

    }]

}

我试过使用 Lodash's partialRight,但它似乎不适用于可迭代对象,也尝试过 flatMap,但没有用。如果有人帮助我解决这个问题,那就太好了。提前致谢!


慕少森
浏览 152回答 2
2回答

温温酱

你可以使用 vanilla javascript 而不是 Lodash 来做到这一点。寻找像 map/reduce/filter 这样的 ES6 方法。例子 :const input = {"id": 2,"full_name": "Karan Kumar","user_skills": [   {      "skill_id": 1,      "skill": {          "skill_name": "Angular 8"       }   },   {      "skill_id": 3,      "skill": {          "skill_name": "Java Spring"       }    }],"resource_allocateds": [  {     "project_id": 1,     "end_date": "2019-06-20",     "project": {         "project_name": "Inventory System"      }  },  {      "project_id": 2,      "end_date": "2020-01-15",      "project": {           "project_name": "Hospital Management System"       }    }  ]};const { id, full_name, user_skills, resource_allocateds } = input;const output = {  id,  full_name,  skills: user_skills.map(item => ({      skill_id: item.skill_id,      skill_name: item.skill.skill_name    })),  resource_allocateds: resource_allocateds.map(item => ({      project_id: item.project_id,      end_date: item.end_date,      project_name: item.project.project_name    })),} 

拉风的咖菲猫

您可以使用toArrayand mapValueslike 如下所示:const jsonData = {&nbsp; "id": 2,&nbsp; "full_name": "Karan Kumar",&nbsp; "user_skills": [&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; "skill_id": 1,&nbsp; &nbsp; &nbsp; &nbsp; "skill": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "skill_name": "Angular 8"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; "skill_id": 3,&nbsp; &nbsp; &nbsp; &nbsp; "skill": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "skill_name": "Java Spring"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; ],&nbsp; "resource_allocateds": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;"project_id": 1,&nbsp; &nbsp; &nbsp; &nbsp;"end_date": "2019-06-20",&nbsp; &nbsp; &nbsp; &nbsp;"project": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"project_name": "Inventory System"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "project_id": 2,&nbsp; &nbsp; &nbsp; &nbsp; "end_date": "2020-01-15",&nbsp; &nbsp; &nbsp; &nbsp; "project": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"project_name": "Hospital Management System"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; ]};const transformedJson = {...jsonData};transformedJson.user_skills = _.toArray(_.mapValues(transformedJson.user_skills, ({ skill_id, skill }) => {&nbsp; return {&nbsp; &nbsp; skill_id,&nbsp; &nbsp; skill_name: skill.skill_name&nbsp; };}));transformedJson.resource_allocateds = _.toArray(_.mapValues(transformedJson.resource_allocateds, ({ project_id, project }) => {&nbsp; return {&nbsp; &nbsp; project_id,&nbsp; &nbsp; project_name: project.project_name&nbsp; };}));document.getElementById('jsonResults').innerHTML = JSON.stringify(transformedJson, null, 2);<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.19/lodash.min.js"></script><pre id="jsonResults"></pre>
随时随地看视频慕课网APP

相关分类

Java
我要回答