将嵌套对象的对象转换为数组嵌套对象的对象

时间:2020-03-17 17:56:44

标签: javascript ecmascript-6

我希望标题和描述不要太混乱。

我所拥有的:一个包含两个父对象(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',
    },
  ],
};

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

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",
    },
  },  
]
*/

2 个答案:

答案 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})

答案 1 :(得分:2)

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

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