Alter and Assign Object Without Side Effects

前端 未结 7 1369
盖世英雄少女心
盖世英雄少女心 2020-11-28 14:27

I currently have the following code:

var myArray = [];
var myElement = {
  id: 0,
  value: 0
}

myElement.id = 0;
myElement.value = 1;
myArray[0] = myElement         


        
7条回答
  •  -上瘾入骨i
    2020-11-28 14:54

    You either need to keep creating new objects, or clone the existing one. See What is the most efficient way to deep clone an object in JavaScript? for how to clone.

提交回复
热议问题