2

I have a csv data.

DATA :

Time,Count
1377973800,293
1377975600,212
1377977400,129
1377979200,89
1377981000,54
1377982800,21
1377984600,15

I want to return the data in this format.

{
"946705035":4,
"946706692":4,
"946707210":0,
"946709243":2,
"946710714":5,
"946712907":3,
"946713183":4,
"946719001":0
}

I do not want the header Time and Count to be appeared in the json format.

Tried using d3.nest() but the result I got like it starts with key variable which i don't want.

Someone please help me in getting the data in that format.

Unknown User
  • 3,598
  • 9
  • 43
  • 81

1 Answers1

1

I believe a code similar to this would do the job:

d3.csv("data.csv", function(error, data) {
    var myObject = {};
    for (var i=0; i < data.length; i++)
        myObject[data[i].Time] = data[i].Count;
};

This gives you data about counts as strings, and if you want numbers, you can just add a "+", which will trigger conversion from string to number:

        myObject[data[i].Time] = +data[i].Count;

EDIT: Here is related question on creating object properties dynamically, maybe you can find something useful there too.

Community
  • 1
  • 1
VividD
  • 10,456
  • 6
  • 64
  • 111