Assign dynamic values to JavaScript dictionary

34 Views Asked by At

I need to assign dynamic values to JavaScript dictionary to be used to echarts but when assigning the dynamic values to dictionary it is showing the different behavior as compare to assigning the static values.

when assigning static values :

     response = {
            "stacks": {"5G_L2": [{"Open": "43"},{"Close": "24"}],
                       "5G_L3": [{"Open": "12"},{"Close": "2"}]
                      }
                  }; 

Debug window showing like :

enter image description here

whereas when assigning the values dynamically like as below :

            var datastck=[];
            var serverdata = '{{ barCdata | tojson }}';
            resPbar = $.parseJSON(serverdata);

            $.each(resPbar, function (i, item) {
              var di={};
              di[item.Team]=[{"Open": item.Open},{"Close": item.Close}];
              datastck.push(di);
            });

            
          response = {
            "stacks": datastck
                  };

Debug window be like :

enter image description here

it is adding one extra layer of array .I need to have the static assignment structure to support the echarts, Someone Advice please?

Thanks in Advance.

1

There are 1 best solutions below

2
loremus On

Try this:

var serverdata = '{{ barCdata | tojson }}';
resPbar = $.parseJSON(serverdata);

var response = {
    "stacks": {}
};

$.each(resPbar, function (i, item) {
    response.stacks[item.Team].push({
        "Open": item.Open,
        "Close": item.Close
    });
});