Skip to content

Instantly share code, notes, and snippets.

@gf3
Forked from inimino/Relations.js
Created Apr 27, 2010
Embed
What would you like to do?
/*
userData = {
'person1' : [1,2,3,4],
'person2' : [3,4,5,6]
}
function returns
{
'person1': {'person2': [3, 4]},
'person2': {'person1': [3, 4]}
}
*/
function calculateRelations(userData) {
sys.puts('calc');
var start = new Date().getTime() / 100;
var matches = {};
for (key in userData) {
matches[key] = {};
for (subKey in userData) {
if (key !== subKey) {
for (var i = 0, l = userData[key].length; i < l; i++) {
for (var u = 0, le = userData[key].length; u < le; u++) {
if (userData[key][i] === userData[subKey][u]) {
if (!matches[key][subKey]) {
matches[key][subKey] = [];
}
matches[key][subKey].push(userData[subKey][u]);
}
}
};
}
}
}
var end = new Date().getTime() / 100;
sys.p('Calculation took: ' + (end - start) + 'sec');
return matches;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment