8

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.

3
  • 3
    let result = arr.map(a => a[0]) Commented Nov 29, 2018 at 20:12
  • 2
    No need for lodash in this case Commented Nov 29, 2018 at 20:13
  • Lodash version: let result = _.map(arr, a => a[0]); Commented Nov 29, 2018 at 20:14

5 Answers 5

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>

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

3 Comments

Lol why do they have a method to get the first element of an array?
maybe for headers of tables?
Head is very useful! It comes from functional languages like lisp, scheme... you can usually add an argument to tell how many of the first elements you want, very basic but useful filtering. Also it’s Used in R, Python with data structures sushi as data frames.
3

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])

Comments

2

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.

Comments

0

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.

Comments

0

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>

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.