Note:
- Before asking this question I looked for some stackoverflow questions like How to determine equality for two JavaScript objects? but they don't help in my case since I'm not looking to check equality between objects but "relevancy"!
My need
I'm working in a project having a module which should suggest zero or one profile (person) to the logged user.
All profiles saved in the database (except the logged user & limit 100 & select randomly) are returned via an API (http://example.com/suggest) as a JSON result and the structure of each profile is like this:
{
name: <userName>,
age: <userAge | default 0>,
language: <userlanguage | default en>,
hobbies: <anArrayOfHobbies>
}
The objectif is to suggest to the current logged user the "most relevent profile" based on:
Properties (in my case only age, language and hobbies).
How much the property is important for the logged user.
Example
Let's say that the API returned this result:
and we suppose that the logged user has this profile
This user can use an internal form to search for the "most relevant profile". This form let him/her associate percents of importance to each property (SUM should be equal 100%).
- Case 1
I'm looking for the profile who has the same age (the closest one) as me.
The intended result is {name: "Jane Doe", ...}
- Case 2
The most important for me in this case is the language.
The intended result is {name: "Jane Roe", ...}
- Case 3
The most important in this case is the number of hobbies that a profile share with me
The intended result is {name: "Jane Doe", ...}
What already did
This is what I already developed:
var Profile = {
name: "",
age: 0,
language: "en",
hobbies: [],
getmostRelevantProfile: function(listOfProfiles){
var mostRelevantProfile = {
age: {value: null, profile: null},
language: [],
hobbies: []
};
listOfProfiles.forEach((profile)=>{
//console.info(profile);
//No need to check profile.name
//Check relevent profile.age
var differencAgeBetweenMeAndCurrentProfile = this.age - profile.age;
if(differencAgeBetweenMeAndCurrentProfile<0){
differencAgeBetweenMeAndCurrentProfile *= -1;
}
var differencAgeBetweenMeAndmostRelevantProfile = this.age - mostRelevantProfile.age.value;
if(differencAgeBetweenMeAndmostRelevantProfile<0){
differencAgeBetweenMeAndmostRelevantProfile *= -1;
}
if( mostRelevantProfile.age.value === null || differencAgeBetweenMeAndCurrentProfile < differencAgeBetweenMeAndmostRelevantProfile ){
mostRelevantProfile.age.value = profile.age;
mostRelevantProfile.age.profile = profile;
}
//Check if this profile speakes the same language as me
if( profile.language === this.language ){
mostRelevantProfile.language.push(profile);
}
//Check if I'm sharing some hobbies with this profile
if(this.hobbies.some((hobby)=>{
if( profile.hobbies.indexOf(hobby) > -1 ){
return true;
}
return false;
})){
mostRelevantProfile.hobbies.push(profile);
}
});
//If at least one profile saved, return it
if( mostRelevantProfile.age.value !== null ){
return mostRelevantProfile;
}
return null;
}
}
var me = Object.create(Profile);
me.name = "John Doe";
me.age = 77;
me.language = "es";
me.hobbies = ["music", "boating", "cooking", "drawing"];
var johnRoe = Object.create(Profile);
johnRoe.name = "John Roe";
johnRoe.age = 20;
johnRoe.language = "fr";
johnRoe.hobbies = ["basebal"];
var janeDoe = Object.create(Profile);
janeDoe.name = "Jane Doe";
janeDoe.age = 43;
janeDoe.language = "de";
janeDoe.hobbies = ["stronomy", "music", "drawing"];
var janeRoe = Object.create(Profile);
janeRoe.name = "Jane Roe";
janeRoe.age = 76;
janeRoe.language = "es";
janeRoe.hobbies = ["stronomy", "music", "walking"];
var mostRelevantProfileForMe = me.getmostRelevantProfile([johnRoe, janeDoe, janeRoe], {age: 13, language: 64, hobbies: 23});
console.info("mostRelevantProfileForMe:", mostRelevantProfileForMe);
What is missing (my question)
- How can I add the percent of importance to select the right profile and then how can I filter the
mostRelevantProfileForMe
to keep only "the right" profile? Is there a better approach to achieve this task?