user4481538
user4481538

Reputation:

UnderscoreJS - Retrieve specific keys from an object within a object

I have the follow object that I'm trying to retrieve the token and UserEmail from Details

var obj = {
  "id": null,
  "firstName": null,
  "lastName": null,
  "createdAt": "2016-10-05T18:16:07.000Z",
  "updatedAt": "2016-10-05T18:16:07.000Z",
  "Details":
    {
      "id": 1,
      "token": null,
      "deviceId": null,
      "code": 12345678,
      "verified": null,
      "createdAt": "2016-10-05T18:16:07.000Z",
      "updatedAt": "2016-10-05T18:16:07.000Z",
      "UserEmail": "[email protected]"
    }
 }

I tried this but I'm getting a blank?

_.pick(_.pick(obj, 'Details'), 'code', 'UserEmail');

Upvotes: 0

Views: 67

Answers (3)

Andy
Andy

Reputation: 525

If you have strictly set Details key in your object then I see the following way:

function getUserDetails (obj) {
     return obj.Details;
}

_(getUserDetails(obj)).pick('userEmail', 'token');

Upvotes: 0

Praveen Prasannan
Praveen Prasannan

Reputation: 7123

_.pick(obj.Details, 'code', 'UserEmail')

Upvotes: 0

Alberto Rivera
Alberto Rivera

Reputation: 3752

Your inner pick will return you an object with a Details key, which you'll then have to access. Instead, do something like:

_.pick(obj['Details'], 'code', 'UserEmail')

Upvotes: 0

Related Questions