So I found a problem in an article (can't find the link or I'd have the solution) that asked the following question.
Given some user data sort users by skill. The following is the data
const namesAndSkills = {
// Dev : Skills
mark : ["javascript","react","java"],
rob : "javascript",
patrick : ["javascript", "react", "python"]
}
Solution(expected output):
Javascript: ["mark", "rob", "patrick"],
react: ["mark", "patrick"],
java: mark,
python: patrick
My solution has only been able to separate the skills from the users, but I can't seem to find a way to compare the users with the skills.
const data = {
// // Dev : Skills
mark: ["javascript", "react", "java"],
rob: "javascript",
patrick: ["javascript", "react", "python"]
}
function swapNamesAndSkills(object) {
let data = Object.entries(object); //Was thinking of using the separated entries to compare, but couldn't evaluate correctly.
//Create a map to store our end key / values
let skillsToNames = new Map();
let skills = [].concat(...Object.values(object));
for (let i = 0; i < skills.length; i++) {
//if map doesnt have the skill as a key, add skill
if (!skillsToNames.has(skills[i])) {
//array of names that have the skill
skillsToNames.set(skills[i], []);
}
}
console.log(skillsToNames);
}
swapNamesAndSkills(data);
As you see I was looking at ways forward, but can't seem to land a solid way to reverse the data. Solution doesn't have to solve the problem, I'm just stuck and can't find my way to the finish line.