javascript - Node 中的 Map-Reduce

标签 javascript node.js asynchronous mapreduce async.js

我有我的 array=[{id:"1"}, {id:"2"}, {id:"3"}] 并希望将其转换为对象:{"1":true, "2", true, "3":true}; 所以我做了以下事情:

async.reduce(myArray, {}, function (memo, item, callback) {
    memo[item.id] = true;
    callback(null, memo);
}, function (err, myObj) {
    console.log("converted object", myObj);
}

它可以完成工作,但是我不确定这在性能方面是否有意义还是毫无意义? Here他们建议使用mapreduce,但我仍然很困惑如何使用map和reduce来提高性能;如有任何帮助,我们将不胜感激。

谢谢

最佳答案

就您而言,您的 reduce 函数中没有任何异步内容,因此您最好使用常规的 Array.prototype.reduce:

var array = [{id:"1"}, {id:"2"}, {id:"3"}];
var newArray = array.reduce(function(cur, val) {
    cur[val.id] = true;
    return cur;
}, {});

console.log(newArray);
// => { '1': true, '2': true, '3': true }

关于javascript - Node 中的 Map-Reduce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29479922/

相关文章:

javascript - 从 Redux reducer 中的对象复制原型(prototype)函数

node.js - 从 hapi 路由中的异步函数返回

node.js - 使用nodemailer发送邮件

javascript - 如何异步加载图像?

javascript - 当用户单击添加更多时复制选择/选项框

javascript - 如何确保我不会错过 WebSockets 上的 "open"事件?

asynchronous - 在 Meteor.call 回调中对 sys.exec 的 Meteor 回调

asynchronous - rust future ::选择循环中修改局部变量

javascript - 如何访问 <input type=file> 内容?

mysql - Node JS、Express MySQL ES_PARSE_ERROR... "right syntax to use near ' ?,?,?,?,?"