-5

How can I capture the value of an object at the current state of the line of code and use that value later instead of the reference?

How can I keep the value of myRows as 1 so I can use it for comparison??

var myRows= this.grid.getSelectedRows();  // 1
var newRows= myRows;

  //run some logic to add 8 to newRows


 alert(myRows)  // 9

 alert(newRows)  //9
Doc Holiday
  • 9,928
  • 32
  • 98
  • 151

2 Answers2

0

You need to copy each of the attributes of the object recursively.

The easiest way is to just use some framework like lodash or angularjs.

plc
  • 864
  • 8
  • 20
0

My suggestion is that you make a deep copy of your JavaScript object. This is usually referred to as cloning an object.

See What is the most efficient way to deep clone an object in JavaScript? on various methods how to achieve this.

Community
  • 1
  • 1
mvw
  • 5,075
  • 1
  • 28
  • 34