是否有一种方法可以避免第5行和第6行中的competenceList[competenceKey]
(对象)重复,而在不使用变量声明的情况下保持相同的对象返回?
const func = ({
entities: { competence: competenceList },
timesheet: { management: { competences: competenceKey } },
}) => ({
employeeKey: competenceList[competenceKey].employee,
payrollEnd: competenceList[competenceKey].competenceEnd,
});
答案 0 :(得分:0)
是的,使用计算所得的属性名称在技术上是可行的:
const func = ({
timesheet: { management: { competences } },
entities: { competence: { [competences]: competence },
}) => ({
employeeKey: competence.employee,
payrollEnd: competence.competenceEnd,
});
但是我不建议您这样做,因为它不太可读。只是写
function func({timesheet, entities}) {
const competence = entities.competence[timesheet.management.compentences];
return {
employeeKey: competence.employee,
payrollEnd: competence.competenceEnd,
};
}