0

I have this sort of data, which we usually grab from api, and it is my first time using lodash.

{
  "_id": "93866",
  "stats": [
    {
      "points": 86,
      "pos": 11,
      "createdAt": "2019-01-16T10:13:40.650Z",
      "updatedAt": "2019-01-16T10:13:40.825Z"
    },
    {
      "points": 79,
      "pos": 26,
      "createdAt": "2019-01-16T10:13:40.650Z",
      "updatedAt": "2019-01-16T10:13:40.825Z"
    },
    {
      "points": 64,
      "pos": 39,
      "createdAt": "2019-01-16T10:13:40.650Z",
      "updatedAt": "2019-01-16T10:13:40.825Z"
    }
  ]
}

All I want is to achieve like this

[ [ 86, 11 ], [ 79, 26 ], [ 64, 39 ] ]

I can simply do this in a native way by doing this,

var result = data.stats.map(o => [o.points, o.pos])
// => [ [ 86, 11 ], [ 79, 26 ], [ 64, 39 ] ]

which yields the expected result but the environment I'm working depends on the use of lodash.

I have tried so far _.map(data.stats, 'points') but Im not sure how I make it to achieve like this [ [ 86, 11 ], [ 79, 26 ], [ 64, 39 ] ]

So how to do it?

4 Answers 4

4

You can use object#destructring in the function callback.

let data = {"_id":"93866","stats":[{"points":86,"pos":11,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"},{"points":79,"pos":26,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"},{"points":64,"pos":39,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"}]},
    result = _.map(data.stats, ({points, pos}) => [points,pos]);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

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

Comments

2

You can use lodash's _.at() with _.map():

const data = {"_id":"93866","stats":[{"points":86,"pos":11,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"},{"points":79,"pos":26,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"},{"points":64,"pos":39,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"}]};

const result = _.map(data.stats, o => _.at(o, ['points', 'pos']));

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

You can also use the lodash/fp versions of _.map() and _.at() to create function:

const data = {"_id":"93866","stats":[{"points":86,"pos":11,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"},{"points":79,"pos":26,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"},{"points":64,"pos":39,"createdAt":"2019-01-16T10:13:40.650Z","updatedAt":"2019-01-16T10:13:40.825Z"}]};

const fn = _.map(_.at(['points', 'pos']));

const result = fn(data.stats);

console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

Comments

1

Here is:

_.map(data.stats, obj => [ obj.points, obj.pos ])

Edit: @Hassan Imam made an interesting answer using destructuring object

Comments

1

You've pretty much figured it out, you just have to combine your native version with your lodash attempt:

var result = _.map(data.stats, o => [o.points, o.pos]);

As you can see, I replaced the second parameter to _.map (which was 'points') with the function used in your native version.

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.