1

Following is JSON notation returned by Response::json() function from Laravel:

{
    "area":["zone1","zone2","zone3"],
    "target":["7.91","4.95","2.95"],
    "sales":["12.35","6.99","4.13"]
}

How can i get 'area' as category, target and sales as series for highcharts(column) using javascript?

For example, how can i get following:

 var cat=['zone1','zone2', 'zone3'];
 var series1=[7.91, 4.95,2.95];
 var series2=[12.35,6.99,4.13];
0

2 Answers 2

1

There are a few ways you can do this. If you are using pure javascript you can do something like

cat = new Array();
series1 = new Array();
series2 = new Array();
for (var value in data["area"])
{
    cat.push(data["area"][value]);
}

for (var value in data["target"])
{
    series1.push(data["target"][value]);
}

for (var value in data["sales"])
{
    series2.push(data["sales"][value]);
}

Or if you want a solution using jQuery then you can do something like:

var cat = $.map(data["area"], function(value,key) { return value; });
var series1 = $.map(data["target"], function(value,key) { return value; });
var series2 = $.map(data["sales"], function(value,key) { return value; });

Both solutions will give you an output of:

["zone1", "zone2", "zone3"]
["7.91", "4.95", "2.95"]
["12.35", "6.99", "4.13"]

Edit

So the data I used was the same JSON object that you provided where every value is a string, but your desired output (and the output Highcharts will want) is the series data in Numeric form so you will need to take that into account.

Sign up to request clarification or add additional context in comments.

1 Comment

Easiest is jQuery and perfect solution. Better than .each() and for.
0

I guess it can help you.

var data = {
  "area":["zone1","zone2","zone3"],
  "target":["7.91","4.95","2.95"],
  "sales":["12.35","6.99","4.13"]
};

var cat = data.area;
var series1 = data.target;
var series2 = data.sales;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.