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

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

對(duì)象值的笛卡爾積(到數(shù)組)

對(duì)象值的笛卡爾積(到數(shù)組)

手掌心 2021-04-09 14:15:28
假設(shè)我有const item = {  "Key1": "Value1",  "Key2": ["Value2", "Value3"]}如何將其轉(zhuǎn)換為[["Value1","Value2"],["Value1","Value3"]] ?我的做法是:let array = [];for (let i of item.Key2){  array.push([item.Key1,i])}只是想知道還有沒有更短的方法?
查看完整描述

3 回答

?
紅顏莎娜

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

// What question mentions

const item = {

  "Key1": "Value1",

  "Key2": ["Value2", "Value3"]

}


let res = item.Key2.map(i => [item.Key1, i])

console.log(res)

// What the world wants!

const item = {

  "Key1": ["ValueA", "ValueB"],

  "Key2": ["Value1", "Value2"]

}


res = item.Key1.reduce((acc, i1) => acc.concat(item.Key2.map(i2 => [i1, i2])), [])

console.log(res)


查看完整回答
反對(duì) 回復(fù) 2021-04-29
?
慕田峪7331174

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

您可以構(gòu)建笛卡爾積。


const

    item = { Key1: "Value1", Key2: ["Value2", "Value3"] },

    values = Object.values(item).map(v => [].concat(v)),

    result = values.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));


console.log(result);


查看完整回答
反對(duì) 回復(fù) 2021-04-29
?
慕慕森

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

const item = { Key1: "Value1", Key2: ["Value2", "Value3"]};


let target = item.Key2.reduce((i, _) => {

  i.push([item.Key1, _]);

  return i;

}, []);


console.log(target)


查看完整回答
反對(duì) 回復(fù) 2021-04-29
  • 3 回答
  • 0 關(guān)注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報(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)