简体   繁体   中英

Lodash map values in a nested array

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?

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> 

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> 

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.

Here is:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM