Q:

Update array item in the reducer

In my reducer state I have array
grounds: [
   {
     name: 'name1',
     id: 123
   }
   {
     name: 'name2',
     id: 456
   }
   {
     name: 'name3',
     id: 789
   }

]

I am dispatching an action that looks like this
export const renameGound = (newName, id) => {
    return{
        type: RENAME_GROUND,
        newName: newName,
        id: id
    }
} 

What I want is to update name value to object that has the id that i get as an action.id. This is what I have tried and it does not work.
case actionTypes.RENAME_GROUND:
            return{
                ...state,
                grounds: state.grounds.forEach(el => {
                    if(el.id === action.id){
                        el.name = action.newName
                    }
                })
            }  

I have mistakenly instead of a grounds in the case above putted loader, and it works, I do not know how but it works. But when i try it like this it doesnt....
How can I update the right object correct way? 

A:

Could you try the next code in the reducer?
return {
        ...state,
        grounds: state.grounds.map(el => {
          if (el.id === action.id) {
            return {
              ...el,
              name: action.newName
            };
          }

          return el;
        })
      };

It's very important that the reducer stays pure. You should never mutate its arguments inside.
More information could be found in the Redux documentation

