vivek
vivek

Reputation: 3

convert array in proper format to show in Google chart

i implement google dount chart now I try to set data using ajax. in google don't chart except data like this.

 ['Category', 'Amount'],
 ['Photos',     22],
 ['Videos',      9],
 ['Free Space',  9]

But when I try to make an array like this it's show different format like this.

["title", "receiptAmount", "Expense category", "50"]

so what can I do to convert in above format. i try with so many way to convert this but I didn't get any success on this I show what can I try with the code.

var arrValues = [];
arrValues = ['title', 'receiptAmount'];       
for(var i=0; i<data.length; i++)
{
  arrValues.push(data[i]['title'],data[i]['receiptAmount']);
  dataArray = arrValues;
 }
 console.log(dataArray);

Please help me to solve this?

Upvotes: 0

Views: 78

Answers (2)

ESI
ESI

Reputation: 2047

You need to create array of arrays like this:

// var arrValues = [];
var arrValues = [['title', 'receiptAmount']];       
for(var i=0; i<data.length; i++)
{
  arrValues.push([data[i]['title'],data[i]['receiptAmount']]);
  dataArray = arrValues;
 }
 console.log(dataArray);

Hope it helps!

Upvotes: 0

u_mulder
u_mulder

Reputation: 54831

You need to push array to array:

var arrValues = [];
arrValues.push(['title', 'receiptAmount']);
for(var i=0; i<data.length; i++) {
    arrValues.push(
        [data[i]['title'], data[i]['receiptAmount']]
    );
}
console.log(arrValues);

Upvotes: 1

Related Questions