Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

javascript - Store array inside object

I have a store with office and inside i have the array people. So it look like this/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
    }
}

I created api which return json witrh 3 field - office_id, id of person and name of person. I want to add all people to array inside suitable office object. At this moment it display only 1 person in the office (I have many-to-many relationship) and Firefox (Chrome not) display my an error

offices[(office_id + 1)] is undefined

If I update manually key of protocols, for example protocols[2], all elements display with suitable office. How can I add all people to suitable office?

question from:https://stackoverflow.com/questions/65891948/store-array-inside-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...