Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need More Efficient Javascript Code

Posted on 2014-03-15
3
Medium Priority
?
273 Views
Last Modified: 2014-03-15
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?
0
Comment
Question by:badrhino
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 39931534
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
 
LVL 1

Author Comment

by:badrhino
ID: 39931630
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
 
LVL 83

Expert Comment

by:leakim971
ID: 39931635
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question