javascript - 如何使usestate恢复默认值
问题描述
我有一个这样的使用状态
const [currentApp, setCurrentApp] = useState({})
的输出currentApp
是
{
activity_name_id: ["1"]
ceo_birthday: "1400-03-07"
ceo_gender: "0"
ceo_id_code: "2233323655666666555"
ceo_photo: "/pic_user/userPic_1624200842.jpg"
ceo_photo_id: "51"
charity_id: "14"
charity_verified: "0"
website: null
....
}
该列表取决于填充了多少字段,我想将所有这些设置为'-'
我使用了此代码但不工作
const newCurrentApp = Object.keys(currentApp).reduce((acc, field) => {
acc[field] = ""
return acc
}, {})
解决方案
要将对象的所有值设置为'-'
,您可以直接遍历没有的键reduce
并修改对象。
const currentApp = {
activity_name_id: ["1"],
ceo_birthday: "1400-03-07",
ceo_gender: "0",
ceo_id_code: "2233323655666666555",
ceo_photo: "/pic_user/userPic_1624200842.jpg",
ceo_photo_id: "51",
charity_id: "14",
charity_verified: "0",
website: null
// ...
};
Object.keys(currentApp).forEach(k => currentApp[k] = '-');
console.log(currentApp);
推荐阅读
- java - 如何从 backStack 中删除堆栈?
- c# - 触摸后按钮保持按下状态(突出显示)
- c++ - 为什么我的类的复制初始化不适用于使用字符串文字的 string_view?
- ios - No Such Module 'Alamofire','SwiftyJson','UiKiT'
- php - transform data collection into laravel
- google-apps-script - onSubmit in apps script embedded in the new google site suddenly has issues
- c# - how to convert c struct with void* to c#
- angular - How to define fix numbers and/or chars with ngx-mask
- highcharts - 如何在 HIghcharts 时间轴中突出显示选定的标签?
- regex - 如何在 Linux 中仅用字母之间的下划线替换空格,忽略数字