亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將兩個數組組合成一個嵌套數組?

如何將兩個數組組合成一個嵌套數組?

PHP
小唯快跑啊 2022-10-27 10:51:31
一點背景:在技術面試中被要求回答這個問題,不能,現在我想改進但找不到回答的邏輯。## 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"]]]非常感謝!
查看完整描述

4 回答

?
慕容708150

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"]]]


查看完整回答
反對 回復 2022-10-27
?
浮云間

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);


查看完整回答
反對 回復 2022-10-27
?
料青山看我應如是

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))


查看完整回答
反對 回復 2022-10-27
?
慕標琳琳

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);


查看完整回答
反對 回復 2022-10-27
  • 4 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號