lodash zip()函数通常接受两个或多个数组作为参数。它可以接受数组的数组吗?
例如,给定一个像 var aa = [[1,2,3],[4,5,6]];
这样的对象和 [[1, 4],[2,5],[3,6]]
zip() 必须像 _.zip(aa[0],aa[1])
那样调用。对于包含两个以上元素的数组,在函数调用中键入索引变得重复。
调用 _.zip(aa)
无效。它只是嵌套了原始数组的数组。
最佳答案
您可以使用 apply
展开数组数组或 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)
关于javascript - lodash zip 函数可以处理数组数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38112725/