-4

I have a json response like this,

"meteo_inverter_analysis":{  
    "Id93922.1":{  
        "inverter_id":"Id93922.1", "total_electricity_generated":1567.7910000000002
    },
    "Id93922.2":{  
        "inverter_id":"Id93922.2", "total_electricity_generated":1468.4869999999999
    },
    "Id93922.3":{  
        "inverter_id":"Id93922.3","total_electricity_generated":498.7319999999999
    },
    "Id93922.4":{  
        "inverter_id":"Id93922.4","total_electricity_generated":461.8369999999999
    }

}

from that response i want to create an js array from total_electricity_generated. The array would be: var array = [1567.7910000000002, 1468.4869999999999, 498.7319999999999, 461.8369999999999]

Can anyone help me how to do it? TIA

2

2 Answers 2

1

You can get the keys of the meteo_inverter_analysis and then loop over to that keys to get the property value of Id93922.*

var data = {"meteo_inverter_analysis":{  
    "Id93922.1":{  
        "inverter_id":"Id93922.1",     
         "total_electricity_generated":1567.7910000000002
    },
    "Id93922.2":{  
        "inverter_id":"Id93922.2",
        "total_electricity_generated":1468.4869999999999
    },
    "Id93922.3":{  
        "inverter_id":"Id93922.3",
        "total_electricity_generated":498.7319999999999
    },
    "Id93922.4":{  
        "inverter_id":"Id93922.4",
        "total_electricity_generated":461.8369999999999
    }
}
};

var keys = Object.keys(data.meteo_inverter_analysis);
var arr = keys.map(key => data.meteo_inverter_analysis[key].total_electricity_generated
);

console.log(arr);

Sign up to request clarification or add additional context in comments.

1 Comment

Thats the answer i needed,., Thanks @Ankit Agarwal
1

I think the following example will help you:

 <script>
  var car =new Array();
  var myObj, i;
  myObj = {
 "name":"John",
 "age":30,
 "cars":[ "Ford", "BMW", "Fiat" ]
 };

 for (i = 0; i < myObj.cars.length; i++) {
 car[i]= myObj.cars[i] + "<br>";
 }

</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.