pmirnd pmirnd - 10 months ago 53
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?


You can do this in ES6 with spread operator :

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