1

I have the following obj:

 { Id: '11ea9563-1a4c-c21b-904f-00ff98e8d5ab',
  Email: 'Email',
  Password:
   { type: 'Buffer',
     data:
      [ Buffer value] },
  roles: [ { Name: 'Developer', userroles: [Object] } ],
  Events:
   [ { Id: '11ea9556-c025-39ae-904f-00ff98e8d5ab'} ] }

I want to get the Id, roles.Name and Events.Id with lodash:

_.pick(obj, ['Id', 'roles.Name', 'Events.Id']),

But with the above I only manage to get the Id.

How can I accomplish this with lodash?

3
  • 1
    Maybe the answer at stackoverflow.com/questions/50096091/… can help you out. Commented May 13, 2020 at 23:02
  • @marco-a Thanks for the link but the accepted answer there is working with an array of objects. In my case it is one object but has two array properties. Any ideas? Commented May 13, 2020 at 23:07
  • If you scroll down a little there's a function deepPick in the answer, maybe this function works out for you? Commented May 13, 2020 at 23:07

3 Answers 3

2
const answer = {
   id: obj.Id,
   roles: _.map(obj.roles, 'Name'),
   events: _.map(obj.Events, 'Id')
};

const obj = { 
  Id: '11ea9563-1a4c-c21b-904f-00ff98e8d5ab',
  Email: 'Email',
  Password: {
    type: 'Buffer'
  },
  roles: [ { Name: 'Developer', userroles: [Object] } ],
  Events:
   [ { Id: '11ea9556-c025-39ae-904f-00ff98e8d5ab'} ]
}

answer = {
   id: obj.Id,
   roles: _.map(obj.roles, 'Name'),
   events: _.map(obj.Events, 'Id')
};


console.log(answer);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

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

2 Comments

Thanks for the answer BUT i only want the Name for roles, in this canse Im getting: roles: { '0': { Name: 'Developer', userroles: [Object] } I dont want the userroles prop to be there
Thanks m8. I added my own below but liked yours alot more since roles becomes [Developer, Admin, etc] but in my solution: [{Name:"Developer"}, {Name:"Admin"}, {Name:"Etc"}]
2

From what I have read here, You can't use _.pick for deep picking. Use a combination of _.get and _.map

var _ = require("lodash")

const data = {
  Id: '11ea9563-1a4c-c21b-904f-00ff98e8d5ab',
  Email: 'Email',
  Password: { type: 'Buffer' },
  roles: [
    { Name: 'Developer', userroles: [Object] }
  ],
  Events: [ { Id: '11ea9556-c025-39ae-904f-00ff98e8d5ab' } ]
}

var result = {
  Id: data.Id,
  Email: data.Email,
  'roles.Name': _.map(data.roles, 'Name'), /* Returns expected array of object values */
  'roles.Name': _.map(data.roles, o => _.pick(o, ['Name'])), /* Returns expected array of object */
  'Events.Id': _.map(data.Events, 'Id'),
};


console.log(result)

2 Comments

_.get isn't necessary here. Just use data.roles instead of _.get(data, 'roles')
This gives perspective on other way to do it. A bit more bloated than @user125661 answer but never the less I learned from this, vote up!
1

Not sure if this is the best answer for this but right now Ive done the following:

Thanks to @marco-a

I analyzed this answer and came up with the following:

const deepPick = (paths, obj) => _.fromPairs(paths.map(p => [_.last(p.split('.')), _.get(obj, p)]));

My own solution so far:

  const data = _.pick(obj, ['Id', 'roles', 'events']);

  const userRoles = data.roles.map(role=> deepPick(['Name'], role));

  const eventIds = data.events.map(eventId=> deepPick(['Id'], eventId));

The output becomes:

{
    "id": "11ea9563-1a4c-c21b-904f-00ff98e8d5ab",
    "roles": [
      {
        "Name": "Developer"
      },
      {
        "Name": "Admin"
      }
    ],
    "events": [
      {
        "Id": "11ea9556-c025-39ae-904f-00ff98e8d5ab"
      }
    ]
  },
}

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.