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

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

如何在javascript中將數組作為對象傳遞?

如何在javascript中將數組作為對象傳遞?

九州編程 2023-07-14 09:55:12
我可以通過這種方式傳遞數組的值及其工作原理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貢獻1783條經驗 獲得超4個贊

您可能想按鍵對值進行分組

  1. 使用forEach循環遍歷每個項目input

  2. 在每個項目中嘗試收集屬于該項目的所有鍵的名稱

  3. 使用for...loop每個鍵的名稱并檢查是否obj[k]為空,然后我們必須分配一個空數組并將此 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);


查看完整回答
反對 回復 2023-07-14
?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

我們可以使用Array.reduce和實現這一點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;

}


查看完整回答
反對 回復 2023-07-14
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

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 )

};


查看完整回答
反對 回復 2023-07-14
?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

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


查看完整回答
反對 回復 2023-07-14
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

使用Array.prototype.reduce,這可以簡單地完成。


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


查看完整回答
反對 回復 2023-07-14
  • 5 回答
  • 0 關注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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