let arr = [];
let obj {case:''};
after I did below code twice,
arr.push(obj)
I got arr below
[{…}, {…}]
0: {case: ''}case:
1: {case: ''}
length: 2[[Prototype]]: Array(0)
and I just want to push certain value (ex. 'a') in first object.
so, I did below code
arr[0].case = 'a'
but, second object is also changed like below
(2) [{…}, {…}]
0: {case: 2}
1: {case: 2}
So, I want to know how to push certain value in certain object in an array.
Objects are stored as references, so to push the object twice and change one of them, you want to push them by:
arr.push(JSON.parse(JSON.stringify(obj)));
If you push your object to your array in this way, you should be able to change one item and not have the other change.