将数组存储在对象中

时间:2021-01-25 20:24:47

标签: javascript reactjs redux frontend

我有一家带办公室的商店,里面有阵列人员。所以它看起来像这样/l

const office = (state = INITIAL_OFFICE, action) => {
    switch(action.type){

        case 'ADD_OFFICE':
            const office = action.item;
            office.people = [];
            return {
                ...state,
                offices: [...state.offices, office],
            }

            case 'ADD_PEOPLE_TO_DEVICE':
            const { office_id, id, name } = action.item
            const offices = [...state.offices];
            offices[office_id+1].people.push({ name })

            return {
                ...state,
                offices
            }
        default:
            return state
    }
}

我创建了 api,它返回带有 3 个字段的 json - office_id、人员 ID 和人员姓名。我想将所有人添加到合适的办公对象内的数组中。此时它只显示办公室中的 1 个人(我有多对多关系)和 Firefox(Chrome 不是)显示我的错误

<块引用>

offices[(office_id + 1)] 未定义

如果我手动更新协议的密钥,例如协议[2],所有元素都会显示为合适的办公室。如何将所有人添加到合适的办公室?

0 个答案:

没有答案
相关问题