I have some javascript code that I am trying to clean up... I originally wrote the definition of an array of objects (called myData) in "brute force" style. The brute force style defines an array of 4 objects (array is always of size 4). My application works fine when defined this way. Code later in the program reads JSON and updates various elments in syntax similar to :
myData[2].Quarter = "Q2";
When I try to clean up/consolidate the definition of myData, my code runs without syntax error, but all 4 elements of the array end up with identical values, where in the brute force style, each object in the array end up with different values. The only thing that is different is the two definitions. This is what I call "brute force", and my entire code set works fine...It is literally the same object copied 4 times.
var myData = [
{
Quarter: 'EMPTY',
Field_Cloud:0,
Field_Cloud_Renew:0,
Field_On_Premise:0,
Field_Total:0,
OD_Cloud:0,
OD_Cloud_Renew:0,
OD_On_Premise:0,
OD_Total:0,
Field_OD_Total:0
},
{
Quarter: 'EMPTY',
Field_Cloud:0,
Field_Cloud_Renew:0,
Field_On_Premise:0,
Field_Total:0,
OD_Cloud:0,
OD_Cloud_Renew:0,
OD_On_Premise:0,
OD_Total:0,
Field_OD_Total:0
},
{
Quarter: 'EMPTY',
Field_Cloud:0,
Field_Cloud_Renew:0,
Field_On_Premise:0,
Field_Total:0,
OD_Cloud:0,
OD_Cloud_Renew:0,
OD_On_Premise:0,
OD_Total:0,
Field_OD_Total:0
},
{
Quarter: 'EMPTY',
Field_Cloud:0,
Field_Cloud_Renew:0,
Field_On_Premise:0,
Field_Total:0,
OD_Cloud:0,
OD_Cloud_Renew:0,
OD_On_Premise:0,
OD_Total:0,
Field_OD_Total:0
}
];
I try to consolidate this down, to what I think should be identical code, but then my application no longer works. The symptom is that all 4 objects of the array end up with identical values.
Consolidated (but broken) code:
var myDataStruct= {
Quarter: 'EMPTY',
Field_Cloud:0,
Field_Cloud_Renew:0,
Field_On_Premise:0,
Field_Total:0,
OD_Cloud:0,
OD_Cloud_Renew:0,
OD_On_Premise:0,
OD_Total:0,
Field_OD_Total:0
};
var myData = [];
myData.push(myDataStruct);
myData.push(myDataStruct);
myData.push(myDataStruct);
myData.push(myDataStruct);
What am I doing wrong, and how can I define myData is a proper consolidated way?