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

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

如何在javascript中將數(shù)組作為對(duì)象傳遞?

如何在javascript中將數(shù)組作為對(duì)象傳遞?

九州編程 2023-07-14 09:55:12
我可以通過這種方式傳遞數(shù)組的值及其工作原理const obj = [  { text1: "John", text2: "male" },  { text1: "Philip", text2: "male" },  { text1: "Matthew", text2: "male" },  { text1: "Richard", text2: "male" }, ];但我需要這樣傳遞var obj = {    text1: ["John", "Philip", "Matthew", "Richard"],    text2: ["male", "male", "male", "male"]};
查看完整描述

5 回答

?
慕娘9325324

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

您可能想按鍵對(duì)值進(jìn)行分組

  1. 使用forEach循環(huán)遍歷每個(gè)項(xiàng)目input

  2. 在每個(gè)項(xiàng)目中嘗試收集屬于該項(xiàng)目的所有鍵的名稱

  3. 使用for...loop每個(gè)鍵的名稱并檢查是否obj[k]為空,然后我們必須分配一個(gè)空數(shù)組并將此 key( ) 的值推kobj[k]。

這就是解決方案

? ? const input = [

? ? ? {text1: 'John', text2: 'male'},

? ? ? {text1: 'Philip', text2: 'male'},

? ? ? {text1: 'Matthew', text2: 'male'},

? ? ? {text1: 'Richard', text2: 'male'},

? ? ];


? ? function toKeyArray(array) {

? ? ? const obj = {};

? ? ? array.forEach(item => {

? ? ? ? const keys = Object.keys(item);

? ? ? ? for (const k of keys) {

? ? ? ? ? (obj[k] = obj[k] || []).push(item[k]);

? ? ? ? }

? ? ? });


? ? ? return obj;

? ? }


? ? const output = toKeyArray(input);

? ? console.log(output);


查看完整回答
反對(duì) 回復(fù) 2023-07-14
?
炎炎設(shè)計(jì)

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

我們可以使用Array.reduce和實(shí)現(xiàn)這一點(diǎn)Object.entries


const obj = [{text1:"John",text2:"male"},{text1:"Philip",text2:"male"},{text1:"Matthew",text2:"male"},{text1:"Richard",text2:"male"},]


const formatData = (data) => data.reduce((res, obj) => {

  Object.entries(obj).forEach(([key, val]) => {

    res[key] = [...(res[key] || []), val];

  });

  return res;

}, {});



console.log(formatData(obj));

.as-console-wrapper {

  max-height: 100% !important;

}


查看完整回答
反對(duì) 回復(fù) 2023-07-14
?
慕少森

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

const input = [

  { text1: "John", text2: "male" },

  { text1: "Philip", text2: "male" },

  { text1: "Matthew", text2: "male" },

  { text1: "Richard", text2: "male" },

 ];


const output = {

    text1: input.map( e => e.text1 ),

    text2: input.map( e => e.text2 )

};


查看完整回答
反對(duì) 回復(fù) 2023-07-14
?
qq_笑_17

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

const obj = [{

    text1: "John",

    text2: "male"

  },

  {

    text1: "Philip",

    text2: "male"

  },

  {

    text1: "Matthew",

    text2: "male"

  },

  {

    text1: "Richard",

    text2: "male"

  },

];


var output = {}

obj.forEach(item => {

  Object.keys(item).forEach(key => {

    if (!output[key]) output[key] = [];

    output[key].push(item[key])

  })

})

console.log(output);


查看完整回答
反對(duì) 回復(fù) 2023-07-14
?
喵喔喔

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

使用Array.prototype.reduce,這可以簡(jiǎn)單地完成。


const obj = [

  { text1: "John", text2: "male" },

  { text1: "Philip", text2: "male" },

  { text1: "Matthew", text2: "male" },

  { text1: "Richard", text2: "male" },

];


const output = obj.reduce((acc, cur) => {

  Object.keys(cur).forEach((item) => {

    acc[item] ? acc[item].push(cur[item]) : acc[item] = [ cur[item] ];

  });

  return acc;

}, {});

console.log(output);


查看完整回答
反對(duì) 回復(fù) 2023-07-14
  • 5 回答
  • 0 關(guān)注
  • 228 瀏覽
慕課專欄
更多

添加回答

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