javascript - 如何向从 fetch 获取的 json 添加另一个属性?
问题描述
我有以下代码:
useEffect(() => {
listAccountsWorker()
.then(res => {
for (account of res) {
// Account is not defined
account.isSelected = false;
}
setAccounts(accounts)
})
.catch(error => {
if (error.status === 401) {
history.push('/login');
}
});
}, []);
我想向数组中的对象添加一个新属性,isSelected
但是出现错误'account' is not defined
。Worker 使用JS
fetch 并在 promise 中返回结果,其内容res
是一个对象数组,例如[{ label: 'hello', ...}, { label: 'bye', ...}]
. 我在这里做错了什么?
解决方案
useEffect(() => {
listAccountsWorker()
.then(res => {
// defined `account`
for (const account of res) {
account.isSelected = false;
}
setAccounts(accounts)
})
.catch(error => {
if (error.status === 401) {
history.push('/login');
}
});
}, []);
推荐阅读
- python - 'str' 和 'int' 的实例之间不支持 '<='
- angular - 如何从 Observable 返回一个数组
- python - 在 Django 中计算来自 Tabel 表单的用户输入
- javascript - React - 将 ref 作为道具发送不工作
- git - 防止 SNAPSHOTS 被合并到 master
- c# - 流架构中的流适配器
- python - 将 python 列表转换为具有相同键但具有不同值的字典
- django - django 从管理面板中选择模板而不重新启动服务器
- json - 如何将 Elastic Search Index 中的所有数据以 JSON 格式导出到指定 _id 字段的文件?
- duplicates - ClickHouse - 将相似条目合并到一个新条目中