进行销毁以避免重复

时间:2019-01-11 15:36:29

标签: ecmascript-6 destructuring spread-syntax

是否有一种方法可以避免第5行和第6行中的competenceList[competenceKey](对象)重复,而在不使用变量声明的情况下保持相同的对象返回?

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

1 个答案:

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