Siddhu
Siddhu

Reputation: 53

How to format data to specific format

I have a local data which is in following format ..

var data = [["Local", 75], ["STD", 55], ["ISD", 96], ["VOIP", 123], ["INCOMING", 34], ["INET", 104]];

Now i want this data to be from database.Here is my database data and this is in json format.

var data2= [{"type":"Local","value":"100"},{"type":"STD","value":"200"},{"type":"ISD","value":"234"},{"type":"VOIP","value":"500"},{"type":"INCOMING","value":"234"},{"type":"INET","value":"123"}]

but i am not able to use this data as this format is not same as my local data ..

So my question is how to convert this to my local data format. Please help.

How to convert var data2 to var data format.

Upvotes: 0

Views: 98

Answers (2)

user3587853
user3587853

Reputation: 1

This is not a valid JSON. A valid one looks like

[{"type":0,"value":0},{"type":1,"value":2},{"type":2,"value":4},{"type":3,"value":6},{"type":4,"value":8}]

You can verify the same on http://jsonlint.com/

Upvotes: 0

Didar_Uranov
Didar_Uranov

Reputation: 1240

Use map() function:

var parsedData = JSON.parse(data2);
data = parsedData.map(function (o) { return [o.type, Number(o.value)]; });
console.log(data);

Upvotes: 1

Related Questions