alexc
alexc

Reputation: 1310

How to manipulate JSON objects into an array with javascript

I'm using the d3.js charting library and I'm using a radar chart extension which seems to only accept the data objects in one way. Instead of trying to change the code for the extension, I thought it would be easier to just manipulate my data into the kind it's coded to accept. If it makes sense?

Anyhow, this is my code;

My JSON:

{
    "questions": ["Staff is courteous and polite","Attentive staff","Modern brand","Innovative brand","Good employer","Company I trust","Place for kids and family","Place for young people","Affordable food"],
    "organizations": ["MC", "BK", "KFC"],
    "dates": ["Jan", "Feb", "Mar"],
    "values": [ [
            [40, 15, 13],
            [25, 24, 14],
            [1, 23, 20]] ... etc etc

Javascript:

d3.json("data.json", function(error, data) {

    var newValue =[];

    var yellow = [
        [
            {"label":"A","value":6},
            {"label":"B","value":4},
            {"label":"C","value":6},
            {"label":"D","value":5.5},
            {"label":"E","value":8},
            {"label":"F","value":7},
            {"label":"G","value":9},
            {"label":"H","value":10},
            {"label":"I","value":3.5}
        ]
    ];
    if (error) {
        console.log(error);
    }
    else {
        data = data;
    }

     var newValue = [];
        var orgS = "MC";
        var dateS = "Jan";
        for (var question = 0; question < data.questions.length; question++) {
            var organization = data.organizations.indexOf(orgS);
            var date = data.dates.indexOf(dateS);
            newValue.push({
                label: data.questions[question],
                value: data.values[question][organization][date]
            });
        }

    console.log(newValue);
    console.log(yellow);


});

console output structure:

enter image description here

So my question is, how can I output my data to the console like the var "yellow" (the bottom one in the pic)?

I've tried to wrap [] around the newValue.push but it didn't return the desired effect.

I'm hoping this is possible, any advice is much appreciated!

Here is a plnk all set up -

https://plnkr.co/edit/EBcxa39sal0PAOJxSYKb?p=preview

(Oh - and I really wasn't sure what an appropriate title for this question should be, please feel welcome to edit/suggest a new one to more accurately describe the problem).

Upvotes: 1

Views: 1101

Answers (1)

j3ff
j3ff

Reputation: 6089

Although I don't see the point ...

d3.json("data.json", function(error, data) {

    var newValue =[];

    var yellow = [
        [
            {"label":"A","value":6},
            {"label":"B","value":4},
            {"label":"C","value":6},
            {"label":"D","value":5.5},
            {"label":"E","value":8},
            {"label":"F","value":7},
            {"label":"G","value":9},
            {"label":"H","value":10},
            {"label":"I","value":3.5}
        ]
    ];
    if (error) {
        console.log(error);
    }
    else {
        data = data;
    }

     var newValue = [];
        var orgS = "MC";
        var dateS = "Jan";
        for (var question = 0; question < data.questions.length; question++) {
            var organization = data.organizations.indexOf(orgS);
            var date = data.dates.indexOf(dateS);
            newValue.push({
                label: data.questions[question],
                value: data.values[question][organization][date]
            });
        }

    var newArrayValue = [];
    newArrayValue.push(newValue);

    console.log(newValue);
    console.log(newArrayValue);
    console.log(yellow);


});

https://plnkr.co/edit/UrmWt5AgqAuJWe8cO1Bi?p=preview

Upvotes: 2

Related Questions