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

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

將兩個數(shù)組轉(zhuǎn)換為一個對象

將兩個數(shù)組轉(zhuǎn)換為一個對象

倚天杖 2023-08-24 17:51:48
我有父親和孩子的名單:const fathers = [    'Bob',    'John',    'Ken',    'Steve'];const children = [    [ 'Mike', 'David', 'Emma' ],    [],    [ 'Harry' ],    [ 'Alice', 'Jennifer' ]];我怎樣才能將它們轉(zhuǎn)換為這樣的對象:const relation = {    Bob: [ 'Mike', 'David', 'Emma' ],    John: [],    Ken: [ 'Harry' ],    Steve: [ 'Alice', 'Jennifer' ]};
查看完整描述

3 回答

?
呼啦一陣風(fēng)

TA貢獻(xiàn)1802條經(jīng)驗 獲得超6個贊

使用Array.prototype.reduce,您可以將它們轉(zhuǎn)換為對象,如下所示。


const fathers = [

    'Bob',

    'John',

    'Ken',

    'Steve'

];


const children = [

    [ 'Mike', 'David', 'Emma' ],

    [],

    [ 'Harry' ],

    [ 'Alice', 'Jennifer' ]

];


const output = fathers.reduce((acc, curV, curI) => ({ ...acc, [curV]: children[curI] }), {});

console.log(output);


查看完整回答
反對 回復(fù) 2023-08-24
?
手掌心

TA貢獻(xiàn)1942條經(jīng)驗 獲得超3個贊

const fathers = ['Bob', 'John', 'Ken', 'Steve'];


const children = [

  ['Mike', 'David', 'Emma'],

  [],

  ['Harry'],

  ['Alice', 'Jennifer']

];


const relation = {};

fathers.forEach((item, index) => {

  relation[item] = children[index];

});

console.log(relation);


查看完整回答
反對 回復(fù) 2023-08-24
?
三國紛爭

TA貢獻(xiàn)1804條經(jīng)驗 獲得超7個贊

2個解決方案:


第一個是聲明一個空對象并使用循環(huán)遍歷每個父對象。


第二種是使用減速機(jī)


var fathers = [

    'Bob',

    'John',

    'Ken',

    'Steve'

];


var children = [

    [ 'Mike', 'David', 'Emma' ],

    [],

    [ 'Harry' ],

    [ 'Alice', 'Jennifer' ]

];


// option 1

  var relations = {};

  fathers.forEach((father, idx) => relations[father] = children[idx])


  console.log(relations);



// option 2

  var relations2 = fathers.reduce((acc, father, idx) => {

      acc[father] = children[idx];

      return acc;

}, {}

)

  console.log(relations2 );


查看完整回答
反對 回復(fù) 2023-08-24
  • 3 回答
  • 0 關(guān)注
  • 233 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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