我有两个型号Book.json和Department.json。当我获取书籍细节时,我们也通过关系获取相应的部门。 输出是:
[{
"title": "Python",
"department": {
"name": "Software"
}
},
{
"title": "World",
"department": {
"name": "Politics"
}
}]
我们如何将上述代码格式化为
[{
"title": "Python",
"department": "Software"
},
{
"title": "World",
"department": "Politics"
}]
当我尝试格式化 ctx.result 时,它会给我一个错误,例如只支持getter。
“message”:“无法设置只有getter的#ModelConstructor属性书”,
答案 0 :(得分:0)
您可以使用Array#map
循环浏览项目并修改迭代中的每个项目:
const data = [{"title":"Python","department":{"name":"Software"}},{"title":"World","department":{"name":"Politics"}}];
const result = data.map(item => {
item.department = item.department.name;
return item;
});
console.log(result);