我有一个名为LessonsGroupedBySections
的对象,它包含保存数组的枚举属性。每个属性键对应于嵌套在id
数组中的对象sections
的值。
我想要的是将LessonsGroupedBySections
键重命名为匹配title
部分。
这是LessonsGroupedBySections
对象:
LessonsGroupedBySections:Object
1:Array[2]
2:Array[2]
null:Array[2]
这是sections
数组:
sections:Array[3]
0:Object
created_at:"2019-03-28 07:45:16"
id:1
series_id:1
title:"First Section"
updated_at:"2019-04-03 08:41:40"
1:Object
2:Object
答案 0 :(得分:0)
您可以根据所需的匹配情况,Object.fromEntries()
来构造具有所需属性名称的对象:
Object.fromEntries(Object.entries(LessonsGroupedBySections).map(lessonsProp => [sections.find(section => section.id == lessonsProp[0]).title, lessonsProp[1]]));
const LessonsGroupedBySections = {
1: [1,2,3],
2: [4,5],
3: [6,7]
};
const sections = [
{id: 1, title: 'First Section'},
{id: 2, title: 'Second Section'},
{id: 3, title: 'Third Section'}
];
const renamedLessons = Object.fromEntries(Object.entries(LessonsGroupedBySections).map(lessonsProp => [sections.find(section => section.id == lessonsProp[0]).title, lessonsProp[1]]));
console.log(renamedLessons);
.as-console-wrapper {
max-height: 100% !important;
top: 0;
}