2

I have a list of objects that I return using webservice. And I try to build an array like you can see below.

$.each(items, function (index, item) { 
 i[index] = '[' + item.Name + ',' + item.Count + ']'; 
});

I need to build array like

var t = [['Mushrooms', 3], ['Onions', 1], ['Olives', 1], ['Zucchini', 1], ['Pepperoni', 2], ];

but it's not working for me for some reason. It should not be string. It needs to has the same format as you see in example. Otherwise it will not work.

Here is the rest of the code I need to add it to table

var t = [['High', 3], ['Onions', 1], ['Olives', 1], ['Zucchini', 1], ['Pepperoni', 2], ];
 var data = new google.visualization.DataTable();
 data.addColumn('string', 'Topping');
 data.addColumn('number', 'Slices');
 data.addRows(t);
asked Jul 16, 2011 at 16:11
2
  • Is i an Array, or a String? Commented Jul 16, 2011 at 16:15
  • Excellent article on this subject here: w3schools.com/js/js_obj_array.asp Commented Jul 16, 2011 at 16:20

2 Answers 2

6

Don't use quotes:

var t = [];
$.each(items, function (index, item) { 
 t[index] = [item.Name, item.Count]; 
});

I also added the code to initialize t (assuming you want that instead of i).

answered Jul 16, 2011 at 16:13
Sign up to request clarification or add additional context in comments.

1 Comment

I need to build exact like you see in example. Otherwise it's not working, just replace names and count var t = [['Mushrooms', 3], ['Onions', 1], ['Olives', 1], ['Zucchini', 1], ['Pepperoni', 2], ];
2
$.each(items, function (index, item) { 
 i.push([item.Name, item.Count]); 
});

remove the ' characters and you will be an actual array.

answered Jul 16, 2011 at 16:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.