0

I dont know if the title is wrong... i dont know which term i should use to exaplain the desired result - the end result i am trying to achieve is this:

var data = [
    {
        value: 300,
        color:"#F7464A",
        highlight: "#FF5A5E",
        label: "Red"
    },
    {
        value: 50,
        color: "#46BFBD",
        highlight: "#5AD3D1",
        label: "Green"
    },
    {
        value: 100,
        color: "#FDB45C",
        highlight: "#FFC870",
        label: "Yellow"
    }
]

I am trying to build this type of array inside a .each loop while getting the values from a form (that can grow in the amount of rows..)

Here is what i got now (which "doesnt work"):

// GET VALUES FROM TR
var cnt = 0;
var datasets = [];
$('.pie-table > tbody > tr').length;
$('.pie-table > tbody > tr').each(function() {

    var ctrid = '#' + $(this).attr('id');
    var size = $(ctrid).find('input#value').val();
    var color = $(ctrid).find('input#color').val();
    var hlight = $(ctrid).find('input#highlight').val();
    var label = $(ctrid).find('input#label').val();



    if (size.length > 0) {
        datasets[cnt] = 'value: ' + size + ', color:"' + color + '", highlight: "' + hlight + '", label: "' + label + '"';
    }

    cnt++;
})

var pieData = [];
for (var key in datasets) {
    pieData.push(datasets[key]);
};

How can i achive a structure similer to the example inside that loop?

2 Answers 2

2

Your object needs to be an object, not a string:

if (size.length > 0) {
  datasets[cnt] = {
    value: size,
    color: color,
    highlight: hlight,
    label: label
  };
}
Sign up to request clarification or add additional context in comments.

1 Comment

This seems right! had to wrap "size" with parseInt - but other than that this works beautifuly! thanks a lot mate.
0

Take a look on this:

if (size.length > 0) {
    // Take Blank Object
    var obj = {};

    // Create keys and assign value
    obj['value'] = size;
    obj['color'] = color;
    obj['highlight'] = hlight;
    obj['label'] = label;

    // Everything complete now push data in array
    datasets.push(obj);
}

1 Comment

Wow, you are fast! i dont know why since chrome doesnt highlight the right line - just highlights some </div> instead but i get a "Uncaught TypeError: undefined is not a function"

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.