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
Andrew Venture
3273 gold badges7 silver badges15 bronze badges
2 Answers 2
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
Matthew Crumley
103k24 gold badges105 silver badges131 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Andrew Venture
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], ];
$.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
zellio
32.8k1 gold badge46 silver badges64 bronze badges
Comments
lang-js
ian Array, or a String?