繁体   English   中英

如何向从 fetch 获取的 json 添加另一个属性?

[英]How can I add another property to the json acquired from fetch?

我有以下代码:

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');
            }
        });
}, []);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM