为了账号安全,请及时绑定邮箱和手机立即绑定

如何在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 关注
  • 144 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信