【发布时间】:2014-10-20 14:21:30
【问题描述】:
我有一个 id 数组,它们是两个数组的交集。我想为交集数组中的每个元素分配一个值。以下是我的不完整代码:
//intersection array
var interArray= ["5ghIJDpPoe3CfHMGu71E6T","69Saih0L7exhpURTx2TN3r","7biThmNOfzA4nZR9h2B6DL",
"51m0PrZokFZNk5b5xptyzC","1XPta4VLT78HQnVFd1hlsK"];
//Edited getting score
function calculateScore (interArray, callback) {
var tmp = 0;
for(i = 0; i < interArray.length; i++) {
tmp++;
}
var score = (tmp/22) * 0.5; //22 for example is the total length of the two arrays that were combined
callback(null, score);
}
如果我想预测两个数组的相似性,这个过程是否正确/有效?
附:我使用 underscore.js 来获取交集,这是在 Node.js API 服务上运行的
谢谢!
编辑
所以基本上我有两个数组。第一个数组是来自用户的 ID 集合。第二个数组是来自用户的 ID 集合。这些 ID 指的是每个项目所引用的项目
async.parallel([
function(callback) {
async.waterfall([getUser,getItems],
function(err, results) {
callback(null, results);
});
},
function(callback) {
async.waterfall([getUsers,getItems],
function(err, results) {
callback(null, results);
});
}
], function(err, results) {
var currentUserArray = results[0]; //item ids from user
var matchedUsersArray = results[1]; //item ids per user
function mapUsersArray () {
//Get the item ids per matchedUser
}
function getIntersection() {
//Get intersection of currentUserArray and matchedUsersArray[i]
}
//Compute similarity/match score of ids of currentUser per matchedUser
});
重述问题:这个程序是否正确或有效?如何计算他们的相似度/匹配分数?由于我映射了matchedUsersArray,我怎样才能再次获取matchedUser详细信息,以便我可以将其作为响应与matchScore一起发送?
【问题讨论】:
-
“相似性”到底是什么意思?请定义您希望代码执行的操作。
-
22 来自哪里?你的函数是怎么知道的?
-
@Bergi 编辑了问题:D
-
@naomik 我已经映射了项目 ids 数组然后得到了它的长度。
标签: javascript arrays node.js underscore.js