Node.JS:按属性映射对象数组,然后获取索引数组的交集

时间:2014-10-19 13:23:27

标签: javascript arrays node.js mongodb underscore.js

我从mongoDB获取了一系列对象,而且我无法通过其属性剖析数组中的每个对象。基本上我想获得一个子文档并得到它的交集,然后为每个交集点的每个索引分配分数。

到目前为止,这是我的代码:

exports.draft = function(req,res) {
    if(req.session.fb_id) fb_id = req.session.fb_id;

    function getUserQuery (fb_id) {
        //Get user
    }
    function getAllUsersQuery (prefs, callback) {
        //Get all users in database from user preferences
    }
    function getUserTracks (callback) {
        var query = getUserQuery;
        query.exec(function(err,tracks){
          callback(null, cu.tracks);
        })
    }

    function getInitialMatches (cuPrefs, callback) {
    var query = getAllUsersQuery(cuPrefs, callback);

    query.exec(function(err, users) {
        if(err) console.log("ERROR retrieving filtered users ---> " + err);
        callback(null, users);
    });
}

function mapInitialMatch (initMatch, callback) {
    var temp = {
        name: initMatch.name,
        tracks: initMatch.tracks
    };
    callback(null, temp);
}

var asyncInitMatches = function (callback) {
    async.waterfall([
        async.apply(getPreferences),
        function (cuPrefs, callback) {
            getInitialMatches(cuPrefs, callback);
        },
        function (initialMatches, callback) {
            async.map(initialMatches, mapInitialMatch, function (err, results) {
                if(err) console.log("ERROR in mapping user" + err);
                callback(null, results);
            });
        }
    ], function(err, results){
        if(err) console.log('------> ERROR in asyncInitMatches --> ' + err);
        callback(null, results);
    });
};

 async.parallel([
    async.apply(getTracks),
    async.apply(asyncInitMatches)
    ], function(err, results) {
       userTracks = results[0];
       matchesTracks = results[1];

       //I would like to get the intersection of userTracks and matchesTracks[i]
 });


}

示例用户跟踪:

  userTracks = {
    "name": "Sample Name", 
    "tracks": [{
         "id": _id,
         "name": "Track name"
       }]
   }

当我在控制台中显示结果时,我总是得到一个null / undefined结果。我也尝试使用underscore.js(map,pluck,intersection)。你能帮我理解async.map和async.each吗?

谢谢!

0 个答案:

没有答案