传播解构以避免重复
Posted
技术标签:
【中文标题】传播解构以避免重复【英文标题】:Spread a destructuring to avoid a duplication 【发布时间】:2019-06-06 13:23:13 【问题描述】:有没有办法避免competenceList[competenceKey]
(对象)在:5
和:6
行中重复,在不使用变量声明的情况下保持相同的对象返回?
const func = (
entities: competence: competenceList ,
timesheet: management: competences: competenceKey ,
) => (
employeeKey: competenceList[competenceKey].employee,
payrollEnd: competenceList[competenceKey].competenceEnd,
);
【问题讨论】:
不要那么努力地避免变量声明。使用它会更具可读性。 是的,我同意你的看法,特别是在这种嵌套场景中。只是出于好奇。 【参考方案1】:是的,使用计算属性名称在技术上是可行的:
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,
;
【讨论】:
以上是关于传播解构以避免重复的主要内容,如果未能解决你的问题,请参考以下文章