4 回答
TA贡献1831条经验 获得超4个赞
一点背景:在技术面试中被要求回答这个问题,不能,现在我想改进但找不到回答的逻辑。
## CASE 1 ##
Given Input:
const profiles = ["Bill", "Steve", "Zuck"]
const skills =
[["Digital Marketing","SEO","UI/UX"],
["Accounting","Digital Marketing","Employer Branding"],
["Accounting","UI/UX"]]
Expected Output:
[[["Accounting"],["Steve","Zuck"]],
[["Digital Marketing"],["Bill","Steve"]],
[["Employer Branding"],["Steve"]],
[["SEO"],["Bill"]],
[["UI/UX"],["Bill","Zuck"]]]
## CASE 2 ##
Given Input:
const profiles= ["First", "Fourth", "Second", "Third"]
const skills =
[["One","Three","Two"],
["One","One three","One two"],
["One two","One two three","Two"],
["One","One three","One two","One two three","Three","Two"]]
Expected Output:
[[["One"],["First","Fourth","Third"]],
[["One three"],["Fourth","Third"]],
[["One two"],["Fourth","Second","Third"]],
[["One two three"],["Second","Third"]],
[["Three"],["First","Third"]],
[["Two"],["First","Second","Third"]]]
TA贡献1829条经验 获得超4个赞
这是一种可能有点昂贵但输出预期结果的方法。
对于您的第二个示例,此代码仍然有效,您可能需要考虑将变量重命名为更通用的名称。
const profiles = ["Bill", "Steve", "Zuck"];
const skills = [
["Digital Marketing","SEO","UI/UX"],
["Accounting","Digital Marketing","Employer Branding"],
["Accounting","UI/UX"]
];
// calculate distinct skills set from skills
const distinctSkills = skills.reduce((acc, cur) => (
acc.concat(cur.filter((v) => (!acc.includes(v))))
), []).sort();
const result = distinctSkills.map((distinctSkill) => {
const people = [];
// for each distinct skill, build the right people array
// based on skills and profiles
skills.forEach((skillSet, index) => {
if (skillSet.includes(distinctSkill)) {
people.push(profiles[index]);
}
});
return [[distinctSkill]].concat([people]);
});
console.log(result);
TA贡献1772条经验 获得超8个赞
const profiles = ["Bill", "Steve", "Zuck"];
const skills = [
["Digital Marketing", "SEO", "UI/UX"],
["Accounting", "Digital Marketing", "Employer Branding"],
["Accounting", "UI/UX"]
];
const combine = (profiles, skills) => {
const indexMap = {};
const output = [];
let index = 0;
//create array of skills and track indexes
skills.forEach(arr => {
arr.forEach(skill => {
if (!indexMap[skill]) {
indexMap[skill] = index.toString();
output.push([
[skill],
[]
])
index++;
}
})
})
//populate secondary arrays using indexmap for reference
profiles.forEach((profile, index) => {
skills[index].forEach(skill => {
let i = indexMap[skill];
output[i][1].push(profile);
})
})
//sort names incase not alphabetical
output.forEach(output => {
output[1].sort((a, b) => a[0] > b[0] ? 1 : -1);
})
// //sort skills incase not alphabetical
return output.sort((a, b) => a[0] > b[0] ? 1 : -1);
}
console.log(combine(profiles, skills))
TA贡献1830条经验 获得超9个赞
您可以首先将所有技能名称收集到一个 Map 中,以这些名称为键,每个名称都有一个关联的空数组作为值。然后再次迭代该数据以将相应的名称推送到这些数组中。然后将这些数组从地图中取出,并在其自己的数组中以技能名称作为前缀。可选择按技能排序。
const profiles = ["Bill", "Steve", "Zuck"];
const skills = [["Digital Marketing","SEO","UI/UX"], ["Accounting","Digital Marketing","Employer Branding"], ["Accounting","UI/UX"]];
let map = new Map(skills.flatMap(row => row.map(s => [s, []])));
skills.forEach((row, i) => row.forEach(s => map.get(s).push(profiles[i])));
let result = Array.from(map.entries(), ([k,v]) => [[k], v])
.sort((a, b) => a[0][0].localeCompare(b[0][0]));
console.log(result);
- 4 回答
- 0 关注
- 97 浏览
添加回答
举报