0

I have an JSON object in this format

var data = [
        { "label" : "January"  }, { "value"  : 10  }, { "barcolor" : "rgba(128, 0, 0, 0.9)" },
        { "label" : "February" }, { "value"  : 20  }, { "barcolor" : "rgba(0, 128, 0, 0.9)" },
        { "label" : "October"  }, { "value"  : 100 }, { "barcolor" : "rgba(0, 0, 128, 0.9)" },
        { "label" : "November" }, { "value"  : 80  }, { "barcolor" : "rgba(255, 0, 0, 0.9)" },
        { "label" : "December" }, { "value"  : 20  }, { "barcolor" : "rgba(0, 255, 0, 0.9)" },
        { "label" : "January"  }, { "value"  : 70  }, { "barcolor" : "rgba(0, 0, 255, 0.9)" }
    ];

I want to print in combine format like

<h1 style="color: rgba(128, 0, 0, 0.9);">January <em>count: 10</em></h1>

these should be 6 h1 or whatever no of "Label", BUT MY jQuery going 18 in a loop

$.each(data, function(index, val) {
    //console.log('index : ' + index + ' || val : ' + val.label);
    $.each(val, function(i, v) {
    //console.log('i : ' + i + ' || v : ' + v);
        var dhtml = '<h1 style="color:' + val.barcolor + '">' + val.label + '<em>' + val.value + '</em></h1>';
        $('body').append(dhtml);
    });
});

not getting desired output http://codepen.io/iahmad/pen/BLgJBb

2
  • 1
    You should change your json Commented Oct 30, 2016 at 16:58
  • 2
    That's not JSON. It's an array of objects. JSON is a data format. Commented Oct 30, 2016 at 17:05

3 Answers 3

1

You should change your JSON format and loop, here is working JS (copy to codepen):

var data = [
    { "label" : "January", "value"  : 10, "barcolor" : "rgba(128, 0, 0, 0.9)" },
    { "label" : "February", "value"  : 20, "barcolor" : "rgba(0, 128, 0, 0.9)" },
    { "label" : "October", "value"  : 100, "barcolor" : "rgba(0, 0, 128, 0.9)" },
    { "label" : "November",  "value"  : 80, "barcolor" : "rgba(255, 0, 0, 0.9)" },
    { "label" : "December", "value"  : 20,  "barcolor" : "rgba(0, 255, 0, 0.9)" },
    { "label" : "January", "value"  : 70,  "barcolor" : "rgba(0, 0, 255, 0.9)" }
];

$.each(data, function(i, val) {
    var dhtml = '<h1 style="color:' + val.barcolor + '">' + val.label + '<em>' + val.value + '</em></h1>'
    $('body').append(dhtml);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Your JSON should be in this format :

var data = [
        { "label" : "January", "value"  : 10, "barcolor" : "rgba(128, 0, 0, 0.9)" },
        { "label" : "February", "value"  : 20, "barcolor" : "rgba(0, 128, 0, 0.9)" },
        { "label" : "October", "value"  : 100, "barcolor" : "rgba(0, 0, 128, 0.9)" },
        { "label" : "November", "value"  : 80, "barcolor" : "rgba(255, 0, 0, 0.9)" },
        { "label" : "December", "value"  : 20, "barcolor" : "rgba(0, 255, 0, 0.9)" },
        { "label" : "January", "value"  : 70, "barcolor" : "rgba(0, 0, 255, 0.9)" }
    ];

And following is the JS code :

$.each(data, function(index, val) {
        var dhtml = '<h1 style="color:' + val.barcolor + '">' + val.label + '<em>' + val.value + '</em></h1>'
        $('body').append(dhtml);
});

Comments

1

Replace your code with this and try:

var data = [
       { "label" : "January"  ,  "value"  : 10  ,  "barcolor" : "rgba(128, 0, 0, 0.9)" },
       { "label" : "February" , "value"  : 20  ,  "barcolor" : "rgba(0, 128, 0, 0.9)" },
       { "label" : "October"  ,  "value"  : 100 ,  "barcolor" : "rgba(0, 0, 128, 0.9)" },
       { "label" : "November" , "value"  : 80  ,  "barcolor" : "rgba(255, 0, 0, 0.9)" },
       { "label" : "December" ,  "value"  : 20  ,  "barcolor" : "rgba(0, 255, 0, 0.9)" },
       { "label" : "January"  ,  "value"  : 70  ,  "barcolor" : "rgba(0, 0, 255, 0.9)" }
        ];

$.each(data, function(i, v) {
var dhtml = '<h1 style="color:' + v.barcolor + '">' + v.label + '<em>' + v.value + '</em></h1>'
$('body').append(dhtml);
                    });

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.