最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can the lodash zip function work with an array of arrays? - Stack Overflow

programmeradmin2浏览0评论

The lodash zip() function normally accepts two or more arrays as arguments. Can it accept an array of arrays?

For example, given an object like var aa = [[1,2,3],[4,5,6]]; and a desired output of [[1,4],[2,5],[3,6]] zip() must be called like _.zip(aa[0],aa[1]). For an array of arrays with more than two elements, typing the indexes into the function call bees repetitive.

Calling _.zip(aa) doesn't work. It just nests the original array of arrays.

The lodash zip() function normally accepts two or more arrays as arguments. Can it accept an array of arrays?

For example, given an object like var aa = [[1,2,3],[4,5,6]]; and a desired output of [[1,4],[2,5],[3,6]] zip() must be called like _.zip(aa[0],aa[1]). For an array of arrays with more than two elements, typing the indexes into the function call bees repetitive.

Calling _.zip(aa) doesn't work. It just nests the original array of arrays.

Share Improve this question asked Jun 30, 2016 at 1:50 Martin BurchMartin Burch 2,9214 gold badges32 silver badges60 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

You can splat your array of arrays using apply or the ES2015 spread operator (...):

// call zip with a `this` context of the lodash object
// and with each entry in aa as a separate argument
// e. g. zip(aa[0], aa[1], ..., aa[N]);
_.zip.apply(_, aa);

// Same call, but using ES2015
_.zip(...aa)

Sure, try this:

_.zip.apply(_, aa)
发布评论

评论列表(0)

  1. 暂无评论