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

Javascript 将嵌套对象的对象转换为数组嵌套对象的对象,javascript,ecmascript-6,Javascript,Ecmascript 6,我希望标题和描述不要太混乱 我拥有的:一个包含两个父对象firstGroup和secondGroup的对象。每个包含子对象 常量数据对象={ 第一组:{ 0: { 标题:“0-firstGroup”, 说明:“第一组中的第0项!”, 新增:2018年, }, 1: { 标题:“1-第一组”, 说明:“第一组中的第一项!”, 新增:2019年, }, 2: { 标题:“2-firstGroup”, 描述:“第一组中的第二项!”, 新增:2020年, }, }, 第二组:{ 0: { 标题:“0-s

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

我拥有的:一个包含两个父对象firstGroup和secondGroup的对象。每个包含子对象

常量数据对象={ 第一组:{ 0: { 标题:“0-firstGroup”, 说明:“第一组中的第0项!”, 新增:2018年, }, 1: { 标题:“1-第一组”, 说明:“第一组中的第一项!”, 新增:2019年, }, 2: { 标题:“2-firstGroup”, 描述:“第一组中的第二项!”, 新增:2020年, }, }, 第二组:{ 0: { 标题:“0-secondGroup”, 说明:“第二组中的第0项!”, 是的, 时间戳:“10:30:25”, }, 1: { 标题:“1-第二组”, 描述:“第二组中的第一项!”, 是的, 时间戳:“14:03:11”, }, }, }; 期望的结果:我希望返回对象的属性是父数组,包含相应的子对象作为元素

所需结果:{ 第一组:[ { 标题:“0-firstGroup”, 说明:“第一组中的第0项!”, 新增:2018年, },{ 标题:“1-第一组”, 说明:“第一组中的第一项!”, 新增:2019年, },{ 标题:“2-firstGroup”, 描述:“第一组中的第二项!”, 新增:2020年, }, ], 第二组:[ { 标题:“0-secondGroup”, 说明:“第二组中的第0项!”, 是的, 时间戳:“10:30:25”, }, { 标题:“1-第二组”, 描述:“第二组中的第一项!”, 是的, 时间戳:“14:03:11”, }, ], }; 额外的结果:如果您愿意尝试一下,我也会对返回的对象的属性感兴趣,这些属性是父对象,包含父标识符的标签,并将子对象作为元素分组数组

结果奖金:{ 第一组:{ 标签:“第一组”, 组:[ { 标题:“0-firstGroup”, 说明:“第一组中的第0项!”, 新增:2018年, }, { 标题:“1-第一组”, 说明:“第一组中的第一项!”, 新增:2019年, }, { 标题:“2-firstGroup”, 描述:“第一组中的第二项!”, 新增:2020年, }, ], }, 第二组:{ 标签:“第二组”, 组:[ { 标题:“0-secondGroup”, 说明:“第二组中的第0项!”, 是的, 时间戳:“10:30:25”, }, { 标题:“1-第二组”, 描述:“第二组中的第一项!”, 是的, 时间戳:“14:03:11”, }, ], }, }; 编辑-我以前的尝试:@RyanWilson说得很好,我应该证明我确实尝试过这个。做了很多尝试,都很糟糕。下面是询问之前的最后一个问题

常数arr=[]; Object.keysdataObject.forEachkey=>{ arr.pushdataObject[键]; }; 控制台。日志'arr',arr; /*日志 [ 0: { 0: { 标题:0-1组 说明:第一组中的第0项! 新增:2018年 }, 1: { 标题:1-1组 说明:第一组中的第一项! 加:2019年 }, 2: { 标题:2-1组 描述:第一组中的第二项! 新增:2020年 }, }, 1: { 0: { 标题:0-secondGroup, 说明:第二组中的第0项!, 是的, 时间戳:10:30:25, }, 1: { 标题:1-2组, 说明:第二组中的第一项!, 是的, 时间戳:14:03:11, }, }, ] */ 您可以使用Objectentries创建带有[key,value]对象对的数组,Arrayreduce重新创建对象

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:'2th item in firstGroup!',added:2020,},},secondgroupnp{title:'0-secondGroup',description:'0th item in secondGroup!',微妙:true,时间戳:'10:30:25',},1:{title:'1-secondGroup',description:'1st item in secondGroup!',微妙:true,时间戳:'14:03:11',},},}; const res=Object.entriesarr.reduceacc[label,group]=>acc[label]={label,group:Object.valuesgroup},acc,{}; console.log{resultsBonus:res}您可以使用Objectentries创建带有[key,value]对象对的数组,Arrayreduce重新创建该对象

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:'2th item in firstGroup!',added:2020,},},secondgroupnp 0:{title:{0:'0-secondgroupgroup',description: “第二组中的第0项!”,精致:true,时间戳:'10:30:25',},1:{title:'1-secondGroup',description:'secondGroup中的第一项!',精致:true,时间戳:'14:03:11',},}; const res=Object.entriesarr.reduceacc[label,group]=>acc[label]={label,group:Object.valuesgroup},acc,{};
log{resultsBonus:res}您可以将嵌套对象分配给数组

常数 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:'2th item in firstGroup!',added:2020},secondgroupnP{title:'0-secondGroup',description:'0th item in secondGroup!',微妙:true,时间戳:'10:30:25'},1:{title:'1-secondGroup',description:'1st item in secondGroup!',微妙:true,时间戳:'14:03:11'}, 结果=Object.fromEntriesObject .entriesdataObject .map[k,v]=>[k,Object.assign[],v] ; console.logresult;
.作为控制台包装{max height:100%!important;top:0;}您可以将嵌套对象分配给数组

常数 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:'2th item in firstGroup!',added:2020},secondgroupnP{title:'0-secondGroup',description:'0th item in secondGroup!',微妙:true,时间戳:'10:30:25'},1:{title:'1-secondGroup',description:'1st item in secondGroup!',微妙:true,时间戳:'14:03:11'}, 结果=Object.fromEntriesObject .entriesdataObject .map[k,v]=>[k,Object.assign[],v] ; console.logresult;
.作为控制台包装{最大高度:100%!重要;顶部:0;}如果您尝试,您的帖子更有可能得到答案。请他人为您编写代码不是本网站的目的。@RyanWilson,但根据出现的答案,本网站可以很好地为我编写代码问题;@TrialAndErrors,我们提供奖金结果也会获得奖金票吗?;@Fabio是的,很遗憾,并不是每个人都遵守这些标准。@RyanWilson是这个网站的新手,对不起。我尝试了很多错误和不好的东西,我将包括最后一次尝试。如果你尝试,你更可能得到你的帖子的答案。要求别人为你写代码不是这个网站的目的。@RyanWilson,但基于回答:这个网站很好地为我写代码问题;@TrialAndErrors,提供奖金结果我们也能得到奖金吗?;@Fabio是的,不幸的是,并不是每个人都遵守这些标准。@RyanWilson是这个网站的新手,对不起。我尝试了很多错误和糟糕的事情,我将包括最后一次尝试。