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 |5 Answers
Reset to default 11You 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
版权声明:本文标题:javascript - Use Lodash to get first element of each array inside array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738603936a2102232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
let result = arr.map(a => a[0])
– ibrahim mahrir Commented Nov 29, 2018 at 20:12let result = _.map(arr, a => a[0]);
– ibrahim mahrir Commented Nov 29, 2018 at 20:14