传播解构以避免重复

Spread a destructuring to avoid a duplication

有没有办法避免在 :5:6 行中重复 competenceList[competenceKey](对象),在不使用变量声明的情况下在 return 上保留相同的对象?

const func = ({
    entities: { competence: competenceList },
    timesheet: { management: { competences: competenceKey } },
}) => ({
    employeeKey: competenceList[competenceKey].employee,
    payrollEnd: competenceList[competenceKey].competenceEnd,
});

是的,使用计算的 属性 名称在技术上是可行的:

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