admin管理员组

文章数量:1201410

This question as a JS-only answer here. But simply because I'd like to become more proficient with Lodash, I'm looking for the Lodash solution.

Let's say I have an array that looks like:

[[a, b, c], [d, e, f], [h, i, j]]

I'd like to get the first element of each array as its own array:

[a, d, h]

What is the most efficient way to do this with Lodash? Thanks.

This question as a JS-only answer here. But simply because I'd like to become more proficient with Lodash, I'm looking for the Lodash solution.

Let's say I have an array that looks like:

[[a, b, c], [d, e, f], [h, i, j]]

I'd like to get the first element of each array as its own array:

[a, d, h]

What is the most efficient way to do this with Lodash? Thanks.

Share Improve this question asked Nov 29, 2018 at 20:11 MegaMattMegaMatt 23.8k39 gold badges114 silver badges160 bronze badges 3
  • 3 let result = arr.map(a => a[0]) – ibrahim mahrir Commented Nov 29, 2018 at 20:12
  • 2 No need for lodash in this case – amd Commented Nov 29, 2018 at 20:13
  • Lodash version: let result = _.map(arr, a => a[0]); – ibrahim mahrir Commented Nov 29, 2018 at 20:14
Add a comment  | 

5 Answers 5

Reset to default 11

You could use _.map with _.head for the first element.

var data = [['a', 'b', 'c'], ['d', 'e', 'f'], ['h', 'i', 'j']],
    result = _.map(data, _.head);

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

Or just the key.

var data = [['a', 'b', 'c'], ['d', 'e', 'f'], ['h', 'i', 'j']],
    result = _.map(data, 0);

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

If you want to use lodash:

const _ = require('lodash')
const arr1 = [[a, b, c], [d, e, f], [h, i, j]]
arr2 = _.map(arr1, e => e[0])

With lodash you can do this with _.first, _.head (_.first is just an alias of _.head) and direct path while mapping through the array:

const data = [['a', 'b', 'c'], ['d', 'e', 'f'], ['h', 'i', 'j']]

console.log(_.map(data, _.first))
console.log(_.map(data, _.head))
console.log(_.map(data, 0)) // direct path
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

If you however have to use lodash just for this then simply use either ES6 or ES5:

const data = [['a', 'b', 'c'], ['d', 'e', 'f'], ['h', 'i', 'j']]

console.log(data.map(x => x[0]))
console.log(data.map(function(x){ return x[0] }))

The performance and actual code is the same practically.

https://lodash.com/docs/#filter

Use the docs, look for some each function.

let arr = [[a, b, c], [d, e, f], [h, i, j]]
let newArray = _.filter(arr, function(subArray){
   return subArray[0] // first element of each subArray
})
console.log(newArray)

This should do it, but I don't understand why you wanna use lodash when pretty much the same filter function exists in vanilla javascript.

You can also use the _.matchesProperty iteratee shorthand, which many lodash methods support.

const data = [['a', 'b', 'c'], ['d', 'e', 'f'], ['h', 'i', 'j']];

const result = _.map(data, '[0]');

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

本文标签: javascriptUse Lodash to get first element of each array inside arrayStack Overflow