Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need More Efficient Javascript Code

Posted on 2014-03-15
3
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

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 82

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
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…

660 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