【发布时间】:2014-08-15 15:52:02
【问题描述】:
如何压缩两个或多个数组,然后按名称而不是索引遍历每个元素?在 Python 中,您可以这样做:
for A, B in zip(As, Bs):
但是,在 Javascript 中,我使用的是这样的:
var As = [1,2,3];
var Bs = [4,5,6];
_(As)
.zip(Bs)
.map(function(x) {
return {A:x[0], B:x[1]}; // I want x[0] and x[1] mapped to names.
})
.map(function(x) {
// Now I can use x.A and x.B.
...
在 Javascript 中有没有更好的方法来做到这一点?
【问题讨论】:
标签: javascript functional-programming underscore.js lodash