Need More Efficient Javascript Code

I have an application that builds 3 AmCharts, each with three series apiece.  I currently have an AJAX call that returns:
[{"CashPrice":"190.30","strDate":"2014-03-15","ChartSet":"1","ChartSeries":"1","LocName":""},{"CashPrice":"190.82","strDate":"2014-03-15","ChartSet":"1","ChartSeries":"2","LocName":""},{"CashPrice":"191.53","strDate":"2014-03-15","ChartSet":"1","ChartSeries":"3","LocName":""},{"CashPrice":"191.51","strDate":"2014-03-22","ChartSet":"1","ChartSeries":"3","LocName":""},{"CashPrice":"190.80","strDate":"2014-03-22","ChartSet":"1",

Open in new window


Where ChartSet is the chart (1-3) and the ChartSeries is the series for chart[x].  AmCharts requires the data to be in this format:
[            {
                    "series1": "xxx",
                    "series2": xxx,
                    "series3": xxx,
                    "date": "date"
                },
                {
                    "series1": "xxx",
                    "series2": xxx,
                    "series3": xxx,
                    "date": "date"
                },....
}

Open in new window


Currently I loop through the return data and put the data into nine different arrays (3 charts x 3 Series):
 
$.each(chartData, function (index, array) {
                  var chartSet = parseInt(array.ChartSet);
                  var series = array.ChartSeries;

                  //Chart 1
                  if (chartSet == 1) {
                      if (series == 1) {
                          arrChart1_Series1.push({
                              date: array.strDate,
                              value: array.CashPrice
                          });
                      } // End Series 1
...Code repeated 8 more times with different array names

Open in new window


Than I have to combine the corresponding arrays (three for each chart) into one array:
 //Chart 1
              for(var i=0; i<arrChart1_Series1.length; i++){
                  chartData1.push({
                      series1: arrChart1_Series1[i].value,
                      series2:arrChart1_Series2[i].value,
                      series3: arrChart1_Series3[i].value,
                      date: arrChart1_Series1[i].date
                  });
...Code Repeated two more times

Open in new window

This obvious takes a lot of time (approx. 6 seconds) and there has to be a better way to do it.  Any body have any better idea's?
LVL 1
badrhinoAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
leakim971Connect With a Mentor PluritechnicianCommented:
Yes, I've an idea but you're not going to like it.
I'm not sure why people want to give such type of task to my lovely language.
Why not using your server with the hundred of Xeon 8 cores this job?

So my idea is : ask the server to send you the json in the right format.
0
 
badrhinoAuthor Commented:
Leakim,

I agree that what I'm trying to do isn't right (that's why I'm here :) ).  If you don't see a better approach in javascript to this , I'll do it server side.   I typically do server side code so I didn't know if I was missing something obvious...
0
 
leakim971PluritechnicianCommented:
No, I'm not here to judge.
Sometimes you can't have the right format (json from third party) so there's not other ways to format at the client side. But when you can, just do it.
It always the same process, do a loop over the data and create the new chart.
I don't see no better approach which really make a difference with your current code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.