P
P
Pogran2016-08-31 11:55:47
React
Pogran, 2016-08-31 11:55:47

How to change object in reducer?

I decided to make the structure through an object (an array is not suitable here) prntscr.com/ccf2v0 . So the question arose of how to correctly change the state of the structure in this case. It's just that both keys and properties can change there. In the reducer for the structure, I get the following values ​​\u200b\u200b(oldValue - old value, newValue - new value, oldKey - old key, newKey - new key)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pogran, 2016-08-31
@Pogran

Made it like this. In principle, it works as it should, the only question is whether this is the best option, because you have to sort through the loop, and if there is a lot of data, then I think it will be bad

let str_entity = {};

      if(action.data.newKey != action.data.oldKey) {
        for(let key in state.structure) {
          if(key == action.data.oldKey) {
            str_entity[action.data.newKey] = action.data.newValue;
          } else {
            str_entity[key] = state.structure[key];
          }
        }
      }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question