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 中,以這些名稱為鍵,每個名稱都有一個關聯的空數組作為值。然后再次迭代該數據以將相應的名稱推送到這些數組中。然后將這些數組從地圖中取出,并在其自己的數組中以技能名稱作為前綴??蛇x擇按技能排序。
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 關注
- 124 瀏覽
添加回答
舉報