Skip to content

Instantly share code, notes, and snippets.

@gf3
Forked from inimino/Relations.js
Created April 27, 2010 18:58
Show Gist options
  • Save gf3/381153 to your computer and use it in GitHub Desktop.
Save gf3/381153 to your computer and use it in GitHub Desktop.
/*
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