pmirnd pmirnd - 1 year ago 85
Javascript Question

Javascript push JSON to Array (Highcharts)

I have this empty array:

var seriesGraficos =[];

and this already made vars:

var dataEnergiaContratadaA = [1458, 1332, 777];
var dataDemandaA = [18, 13, 77];
var nameEnergiaContratadaA = "Energy";
var nameDemandaA = "Demand";

and I need to fill the array like this way (I need the seriesGraficos exactly like this, it's the series array of Highcharts):

seriesGraficos = [{
name: nameEnergiaContratadaA,
data: dataEnergiaContratadaA
}, {
name: nameDemandaA ,
data: dataDemandaA

I was trying something like:

{"name": nameEnergiaContratadaA,"data": dataEnergiaContratadaA},
{"name": nameDemandaA,"data": dataDemandaA}

But I can't get it. Any help?

Answer Source

You can do this in ES6 with spread operator :

seriesGraficos = [];
{"name": 'nameEnergiaContratada',"data": 'dataEnergiaContratadaA'},
{"name": 'nameDemandaA',"data": 'dataDemandaA'}


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download