我有两个相同长度的迭代,我需要同时循环。一个迭代是自定义对象的Map,另一个是对象的数组。我需要将数组的内容添加到Map中(通过一些辅助函数原型函数),最好是异步并发的。而且,这两个容器根据它们的顺序相互关联。因此,数组中的第一个元素需要添加到Map中的第一个元素。同时迭代两个相同长度的迭代
如果我是这样做同步,将是这个样子:
var map;
var arr;
for (var i = 0; i < arr.length; i++) {
// get our custom object, call its prototype helper function with the values
// in the array.
let customObj = map[i];
customObj.setValues(arr[i])
}
通常遍历数组异步并同时我用蓝鸟Promise.map
。这将是这个样子:
var arr
Promise.map(arr, (elem) => {
// do whatever I need to do with that element of the array
callAFunction(elem)
})
这将是真棒,如果我可以做这样的事情:
var map;
var arr;
Promise.map(map, arr, (mapElem, arrElem) {
let customObj = mapElem[1];
customObj.setValue(arrElem);
})
有谁知道图书馆或一个聪明的方式来帮助我做到这一点的?
谢谢。
编辑:只是想添加一些关于存储在地图中的对象的澄清。地图以唯一值为键值,并且值与构成此对象的唯一值相关联。它的定义与此类似:
module.exports = CustomObject;
function CustomObject(options) {
// Initialize CustomObjects variables...
}
CustomObject.prototype.setValue(obj) {
// Logic for adding values to object...
}
这是完美的!在Promise.map文档中忽略了这一点。万分感谢! –