0

I have the following JS code. I am unable to push the Metric variable to MetricArray. What is the issue? Updated to add a loop and a statement that clears the array.

PS: This isn't the complete code and I can't put the exact code here. I fetch different values of Metric and push them to ArrayOfMetric.MetricArray. Then I reset ArrayOfMetric.MetricArray and fetch another set of Metric values and so on. So I need a way to reset ArrayOfMetric. Please also explain why the initial values are getting wiped out when I am printing it while the array had a snapshot of those values.

Is there a way to reset the array without loosing initial values?

for(var i = 0; i < 10; i++) {
    var Metric = {
        "Code": "",
        "Name": "",
        "Value": ""
    };

    var ArrayOfMetric = {
        "MetricArray": []
    };

    Metric.Code = "ABC";
    Metric.Name = "AB Corporation";
    Metric.Value = 1245.67;

    ArrayOfMetric.MetricArray.push(Metric);

    console.log(Metric);        // Prints correct values                 
    console.log(ArrayOfMetric); // Shows MetricArray empty

    ArrayOfMetric.MetricArray = [];   
}  
Forkmohit
  • 733
  • 3
  • 12
  • 31

1 Answers1

0

I looks like you have a syntax error where Console should be console

filype
  • 8,034
  • 10
  • 40
  • 66