0

This i have to admin has always been my Achilles tendon despite my years of experience in programming.

I have a json result looking like this from which I want to draw a time series using highcharts.com

[{"iso-2":"DE","year":"2000","value":"0"},{"iso-2":"FR","year":"2000","value":"0"},{"iso-2":"KE","year":"2000","value":"0"},{"iso-2":"DE","year":"2001","value":"0"},{"iso-2":"FR","year":"2001","value":"0"},{"iso-2":"KE","year":"2001","value":"0"},{"iso-2":"DE","year":"2002","value":"0"},{"iso-2":"FR","year":"2002","value":"0"},{"iso-2":"KE","year":"2002","value":"0"},{"iso-2":"DE","year":"2003","value":"9355"},{"iso-2":"FR","year":"2003","value":"19490"},{"iso-2":"KE","year":"2003","value":"0"},{"iso-2":"DE","year":"2004","value":"0"},{"iso-2":"FR","year":"2004","value":"0"},{"iso-2":"KE","year":"2004","value":"0"},{"iso-2":"DE","year":"2005","value":"11"},{"iso-2":"FR","year":"2005","value":"8"},{"iso-2":"KE","year":"2005","value":"0"},{"iso-2":"DE","year":"2006","value":"2"},{"iso-2":"FR","year":"2006","value":"1388"},{"iso-2":"KE","year":"2006","value":"0"},{"iso-2":"DE","year":"2007","value":"0"},{"iso-2":"FR","year":"2007","value":"0"},{"iso-2":"KE","year":"2007","value":"0"}]

I'd like to dynamically generate the above result into an arrays that looks like this

series: [{
            name: 'KE',
            data: [0,0,0,0,0,0,0,0]
        }, {
            name: 'FR',
            data: [0,0,0,19490,0,8,1388,0]
        }, {
            name: 'DE',
            data: [0,0,0,9355,0,2,0]
        }]

Thank you so much for looking into this

3
  • possible duplicate of Array for Highcharts: Commented Jan 5, 2014 at 15:12
  • I have written and re-written until my fingers hurt. I'm stuck at the loop for creating the series, i've tried a for loop and using jQuery's filter() and map() functions but the logic isn't just coming out. I've checked the link but we're drawing slightly something different but experimenting with the answer. Thanks for writing Commented Jan 5, 2014 at 15:19
  • 1
    I am sure :) What I wanted to say is that some code will help more. Commented Jan 5, 2014 at 15:23

2 Answers 2

2
var gathered = data.reduce(function(prev, curr) {
    if (prev.hasOwnProperty(curr["iso-2"])) {
        prev[curr["iso-2"]].push(parseInt(curr["value"]));
    } else {
        prev[curr["iso-2"]] = [parseInt(curr["value"])];
    }
    return prev;
}, {});

var result = [];
for (var country in gathered) {
    var obj = {};
    obj["name"] = country;
    obj["data"] = gathered[country];
    result.push(obj);
}

console.log(result);

Output

[ { name: 'DE', data: [ 0, 0, 0, 9355, 0, 11, 2, 0 ] },
  { name: 'FR', data: [ 0, 0, 0, 19490, 0, 8, 1388, 0 ] },
  { name: 'KE', data: [ 0, 0, 0, 0, 0, 0, 0, 0 ] } ]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I'd have a case of beer delivered at your door if I had your address!
1

Here's what I can think of, considering the data you have in the JSON is sorted by year:

var json_data = '[{"iso-2":"DE","year":"2000","value":"0"},...]'; // This is the json data you have.
var data = JSON.parse(json_data); // Convert JSON data to javascript object or array

// Organize the data in an object
var organized = {};

data.forEach(function (item){
    if (typeof organized[item['iso-2']] !== 'undefined'){
        organized[item['iso-2']].push(item['value']);
    } else {
        organized[item['iso-2']] = [item['value']];
    }
});

// Convert the object to the format you need
var series = [];

for (i in organized){
    series.push({
        name: i,
        data: organized[i]
    });
}

1 Comment

Also works like a charm. No need to convert to Json as in line 2 because it is already json. I like your solution because i don't need to add extra js

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.