第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將兩個(gè)數(shù)組組合成一個(gè)嵌套數(shù)組?

如何將兩個(gè)數(shù)組組合成一個(gè)嵌套數(shù)組?

PHP
小唯快跑啊 2022-10-27 10:51:31
一點(diǎn)背景:在技術(shù)面試中被要求回答這個(gè)問題,不能,現(xiàn)在我想改進(jìn)但找不到回答的邏輯。## 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貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊

一點(diǎn)背景:在技術(shù)面試中被要求回答這個(gè)問題,不能,現(xiàn)在我想改進(jìn)但找不到回答的邏輯。


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


查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
浮云間

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超4個(gè)贊

這是一種可能有點(diǎn)昂貴但輸出預(yù)期結(jié)果的方法。


對(duì)于您的第二個(gè)示例,此代碼仍然有效,您可能需要考慮將變量重命名為更通用的名稱。


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


查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
料青山看我應(yīng)如是

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊

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


查看完整回答
反對(duì) 回復(fù) 2022-10-27
?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以首先將所有技能名稱收集到一個(gè) Map 中,以這些名稱為鍵,每個(gè)名稱都有一個(gè)關(guān)聯(lián)的空數(shù)組作為值。然后再次迭代該數(shù)據(jù)以將相應(yīng)的名稱推送到這些數(shù)組中。然后將這些數(shù)組從地圖中取出,并在其自己的數(shù)組中以技能名稱作為前綴??蛇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);


查看完整回答
反對(duì) 回復(fù) 2022-10-27
  • 4 回答
  • 0 關(guān)注
  • 136 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)