This question has already been answered:

It is necessary to replace one variable with another one and change the second one. Like this:

a = b; b = c; 

The problem is as follows. It is necessary to change the values ​​of variables in the array. Assume inventory[3]['color'] = 'black', inventory[4]['color'] = 'red' .

 inventory[3] = inventory[4]; inventory[4]['color'] = 'green'; 

Then at the end I get inventory[3]['color'] == 'green' Explain why my method does not work, please

Reported as a duplicate at Grundy. javascript Jan 11 '18 at 9:52 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • If you swap places, try this: [inventory[3], inventory[4]] = [inventory[4], inventory[3]]; Although, this method will not work if, apart from color , there are other properties and they do not need to be affected - Deonis
  • Thank you very much, do not tell me now why my method does not work? - Dmitriy

1 answer 1

Instead

 inventory[3] = inventory[4]; 

Need to

 inventory[3]['color'] = inventory[4]['color']; 
  • So I need to completely replace inventory [4] to the place of inventory [3] - Dmitriy