首页 > 解决方案 > 您作为参数获得的对象中多个键之一的默认值

问题描述

我有一个功能:

const doSomething= (person) => {
// Some code... 
}

获取此对象作为参数:

const person = {
name: 'Daniel',
Age: 34,
City: 'Dresden'
}

city要处理is的情况undefined,我怎样才能只给出这个特定的city键默认值?不是这样的整个对象:

const doSomething= (person = null) => {
// Some code... 
}

标签: javascript

解决方案


不使用person参数,而是将其分解为name,AgeCity,并将默认值分配给City

const doSomething = ({ name, Age, City = 'default city' }) => {
  // Some code... 
}

推荐阅读