【问题标题】:Turn Object of Nested Objects into an Object of Array Nested Objects将嵌套对象的对象转换为数组嵌套对象的对象
【发布时间】:2026-01-31 06:30:01
【问题描述】:

我希望标题和这个描述不会太混乱。

我有什么:一个包含两个父对象(firstGroupsecondGroup)的对象。每个都包含子对象。

const dataObject = {
  firstGroup: {
    0: {
      title: '0-firstGroup',
      description: '0th item in firstGroup!',
      added: 2018,
    },
    1: {
      title: '1-firstGroup',
      description: '1st item in firstGroup!',
      added: 2019,
    },
    2: {
      title: '2-firstGroup',
      description: '2nd item in firstGroup!',
      added: 2020,
    },
  },
  secondGrounp: {
    0: {
      title: '0-secondGroup',
      description: '0th item in secondGroup!',
      delicate: true,
      timestamp: '10:30:25',
    },
    1: {
      title: '1-secondGroup',
      description: '1st item in secondGroup!',
      delicate: true,
      timestamp: '14:03:11',
    },
  },
};

期望的结果:我希望返回的对象的属性是父数组,包含各自的子对象作为元素。

resultsDesired: {
  firstGroup: [
    {
      title: '0-firstGroup',
      description: '0th item in firstGroup!',
      added: 2018,
    },{
      title: '1-firstGroup',
      description: '1st item in firstGroup!',
      added: 2019,
    },{
      title: '2-firstGroup',
      description: '2nd item in firstGroup!',
      added: 2020,
    },
  ],
  secondGrounp: [
    {
      title: '0-secondGroup',
      description: '0th item in secondGroup!',
      delicate: true,
      timestamp: '10:30:25',
    }, {
      title: '1-secondGroup',
      description: '1st item in secondGroup!',
      delicate: true,
      timestamp: '14:03:11',
    },
  ],
};

奖励结果:如果您也愿意尝试一下,我也会对返回的对象的属性感兴趣,这些属性是父对象,包含父标识符的标签和以子对象为元素的组数组。

resultsBonus: {
  firstGroup: {
    label: 'firstGroup',
    group: [
      {
        title: '0-firstGroup',
        description: '0th item in firstGroup!',
        added: 2018,
      }, {
        title: '1-firstGroup',
        description: '1st item in firstGroup!',
        added: 2019,
      }, {
        title: '2-firstGroup',
        description: '2nd item in firstGroup!',
        added: 2020,
      },
    ],
  },
  secondGrounp: {
    label: 'secondGroup',
    group: [
      {
        title: '0-secondGroup',
        description: '0th item in secondGroup!',
        delicate: true,
        timestamp: '10:30:25',
      }, {
        title: '1-secondGroup',
        description: '1st item in secondGroup!',
        delicate: true,
        timestamp: '14:03:11',
      },
    ],
  },
};

编辑 - 我之前的尝试: @RyanWilson 提出了一个很好的观点,我应该证明我确实尝试过这个。做了很多尝试,所有这些都很糟糕。下面是提问前的最后一个……

const arr = [];

Object.keys(dataObject).forEach((key) => {
  arr.push(dataObject[key]);
});

console.log('arr ', arr);
/* LOG
[
  0: {
    0: {
      title: "0-firstGroup"
      description: "0th item in firstGroup!"
      added: 2018
    },
    1: {
      title: "1-firstGroup"
      description: "1st item in firstGroup!"
      added: 2019
    },
    2: {
      title: "2-firstGroup"
      description: "2nd item in firstGroup!"
      added: 2020
    },
  },
  1: {
    0: {
      title: "0-secondGroup",
      description: "0th item in secondGroup!",
      delicate: true,
      timestamp: "10:30:25",
    },
    1: {
      title: "1-secondGroup",
      description: "1st item in secondGroup!",
      delicate: true,
      timestamp: "14:03:11",
    },
  },  
]
*/

【问题讨论】:

  • 如果您尝试一下,您的帖子更有可能得到答复。要求人们为您编写代码不是本网站的目的。
  • @RyanWilson,但根据出现的答案,这个网站很好地解决了“为我写代码”的问题;)
  • @TrialAndErrors,我们是否也因提供奖金结果而获得奖金投票? ;)
  • @Fabio 是的,不幸的是,并不是每个人都遵循这些标准。
  • @RyanWilson 新用户,抱歉。我尝试了很多以错误和错误告终的事情,我将包括最后一次尝试。

标签: javascript ecmascript-6


【解决方案1】:

您可以将嵌套对象分配给数组。

const
    dataObject = { firstGroup: { 0: { title: '0-firstGroup', description: '0th item in firstGroup!', added: 2018 }, 1: { title: '1-firstGroup', description: '1st item in firstGroup!', added: 2019 }, 2: { title: '2-firstGroup', description: '2nd item in firstGroup!', added: 2020 } },secondGrounp: { 0: { title: '0-secondGroup', description: '0th item in secondGroup!', delicate: true, timestamp: '10:30:25' }, 1: { title: '1-secondGroup', description: '1st item in secondGroup!', delicate: true, timestamp: '14:03:11' } } },
    result = Object.fromEntries(Object
        .entries(dataObject)
        .map(([k, v]) => [k, Object.assign([], v)])
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

    【解决方案2】:

    您可以使用Object#entries 来创建带有[key,value] 对象对的数组,然后Array#reduce 重新创建对象

    const arr = { firstGroup: { 0: { title: '0-firstGroup', description: '0th item in firstGroup!', added: 2018, }, 1: { title: '1-firstGroup', description: '1st item in firstGroup!', added: 2019, }, 2: { title: '2-firstGroup', description: '2nd item in firstGroup!', added: 2020, }, }, secondGrounp: { 0: { title: '0-secondGroup', description: '0th item in secondGroup!', delicate: true, timestamp: '10:30:25', }, 1: { title: '1-secondGroup', description: '1st item in secondGroup!', delicate: true, timestamp: '14:03:11', }, }, };
    
    const res = Object.entries(arr).reduce((acc,[label,group])=>( acc[label] = {label,group:Object.values(group)},acc),{});
    
    console.log({resultsBonus:res})

    【讨论】: