How can I convert this:
var expenseList = [[1,"Beverages"],
[2,"Condiments" ],
[3,"Confections" ],
[4,"Dairy Products" ],
[5,"Grains/Cereals" ],
[6,"Meat/Poultry" ],
[7,"Produce" ],
[8,"Seafood" ]];
into this:
output = [
{ value: 1, text: "Beverages" },
{ value: 2, text: "Condiments" },
{ value: 3, text: "Confections" },
{ value: 4, text: "Dairy Products" },
{ value: 5, text: "Grains/Cereals" },
{ value: 6, text: "Meat/Poultry" },
{ value: 7, text: "Produce" },
{ value: 8, text: "Seafood" }
];
The first data source can be taken as input and the second is the required output. I tried converting the array into a type of string using a loop and then parse the string into json but Json.pasre throws error there.
var list = '';
for (var i = 0; i < expenseList.length; i++) {
var showText = expenseList[i][1].replace('"', '\\"');
var key = expenseList[i][0];
list = '{ value: ' + key + ', text: "' + value + '"},' + list;
}
list = '[' + list.substr(0, list.length - 1) + ']';
var bindList;
bindList = JSON.parse(list);