0

Hi trying to create this array dynamically. This is the static way:

var pieData = [
                {
                    value: 300,
                    color:getRandomColor(),
                    highlight: getRandomColor(),
                },
                {
                    value: 50,
                    color: getRandomColor(),
                    highlight: "#fac878",
                },
                {
                    value: 100,
                    color: getRandomColor(),
                    highlight: getRandomColor(),
                },
                {
                    value: 120,
                    color: getRandomColor(),
                    highlight: getRandomColor(),
                }

            ];

This is what I achieved:

$.ajax({
        method: "POST",
        url: "getPieChartData"
    })
    .done(function(data){
        obj  = $.parseJSON(data);
        var pieData = []; i = 0;
        $.each(obj, function(key, item) {
            pieData[i].value = item.total + " - " + item.d_name;
            pieData[i].color = getRandomColor();
            pieData[i].highlight = getRandomColor();
            i++;
        });
    });

I am getting value from my function that is not a problem. My issue is that I am getting in the console that this part:

pieData[i].value = item.total +" - " + item.d_name; TypeError: pieData[i] is undefined

What am I doing wrong? thx

3 Answers 3

3

You can simple use .push() method. No meed to use indexer.

The push() method adds one or more elements to the end of an array and returns the new length of the array.

$.each(obj, function(key, item) {
    pieData.push({
        value: item.total + " - " + item.d_name,
        color : getRandomColor(),
        highlight : getRandomColor()
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

but how can I access after the ajax event ? if I put a console log it said me that is undefined ?
@GeorgeMoldovan, A of AJAX stands for asynchronous go through stackoverflow.com/questions/14220321/… , Its a good read and will provide solution for your problem
This code works good, but "push" isn't the solution of the problem. The solution IS the new object passed to push, but you can do it without push too, like @James Newton wrote in his answer.
2

You need to create the pieData[i] object first;

var pieData = []; i = 0;
$.each(obj, function(key, item) {
    pieData[i] = {}; // added line
    pieData[i].value = item.total + " - " + item.d_name;
    pieData[i].color = getRandomColor();
    pieData[i].highlight = getRandomColor();
    i++;
});

Comments

1

Before pieData[i].value .., you should first have a line:

pieData[i] = {};

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.