I have been sitting with this bug for 2 hours, although it is very banal.

let aa = this.state.containers.map((obj, idx) => { if(obj.mainItem !== null) { print('____________________'); print(newItems[idx]); obj.mainItem.content = newItems[idx]; print(obj.mainItem.content); } if(obj.mainItem !== null) { print(obj.mainItem); } print('______________________'); return obj }); 

It assigns the last element of the array newItems, moreover, if we replace print(obj.mainItem); on print(obj.mainItem.content); (in the second if) outputs correctly: enter image description here

if you combine print(obj.mainItem); print(obj.mainItem.content); print(obj.mainItem); print(obj.mainItem.content); we get: enter image description here From displays the correct value of the field that is occupied, for some reason, by another object.

Tried to do through for in, just for, nothing helps

  • Hover over the blue i and read the warning. Print objects to the console using JSON.stringify - Alexey Ten
  • @AlexeyTen Thank you, the problem was solved with a crutch JSON.parse - Mikhail

1 answer 1

Thanks a lot @AlexeyTen, the problem was solved like this:

 obj.mainItem = JSON.parse(JSON.stringify(obj.mainItem)); obj = JSON.parse(JSON.stringify(obj));