javascript - 您作为参数获得的对象中多个键之一的默认值
问题描述
我有一个功能:
const doSomething= (person) => {
// Some code...
}
获取此对象作为参数:
const person = {
name: 'Daniel',
Age: 34,
City: 'Dresden'
}
city
要处理is的情况undefined
,我怎样才能只给出这个特定的city
键默认值?不是这样的整个对象:
const doSomething= (person = null) => {
// Some code...
}
解决方案
不使用person
参数,而是将其分解为name,
、Age
和City
,并将默认值分配给City
:
const doSomething = ({ name, Age, City = 'default city' }) => {
// Some code...
}
推荐阅读
- amazon-web-services - 云存储中的读/写操作到底是什么?是否可以将多个写入请求合并为一个请求?
- python-3.x - 为 TensorBoard 命名 Keras Tuner Trials 目录
- python - 如何在熊猫数据框中找到重复项?
- opengl-es - 带颜色的 OpenGL DrawArrays
- c# - C# JSON - 具有相同属性的不同类型的多个对象的序列化
- jwt - 在 HTTP API 网关中使用授权方时将 JWT 令牌替换为另一个 JWT 令牌
- javascript - Javascript 类 - 如何避免一遍又一遍地调用方法?
- reactjs - 表单加载时最初不会填充下拉字段
- javascript - Chart.JS - 错误的 Y 轴
- selenium - Capybara + Selenium + Web 抓取 - 并行请求失败