1

I want to display curves of data in JSON format using flot.js but the data file contains two many lines (over than 50,000) and it takes too much time to render the graph. So I would like to keep only one line out of n.

For example for n=3, I want to keep lines with indexes : 1,4,7,10 etc

The JSON data file has the following format :

[{"t":"22.40",
"lumi":"738.00",
"h":"31.20",
"f":"72.32",
"hi":"76.43",
"date":"2015-02-28T13:38:41.025Z",
"_id":"54f1c4e17cb06e5e09015b63"},

... 50,000 other lines

What is the simplest way to achieve this ?

Jeremy Beard
  • 2,727
  • 1
  • 20
  • 25
Harijoe
  • 1,771
  • 1
  • 16
  • 27
  • `var arrData = JSON.parse(jsonData);`, `arrData[0]` – Callum Linington Mar 02 '15 at 15:03
  • possible duplicate of [Remove specific element from an array?](http://stackoverflow.com/questions/5767325/remove-specific-element-from-an-array) – Mark Cidade Mar 02 '15 at 15:10
  • 1
    No actually I want to delete for example lines with index 1,2,4,5,7,8 etc to only keep 1/3 of the lines. I will edit my post to make it clearer. – Harijoe Mar 02 '15 at 15:19
  • Are you pulling data from server side? If so the easy place would to filter it on the server side call with a query variable. – David Nguyen Mar 02 '15 at 15:40
  • Yeah I'm pulling data from server side, but I use monk for that and I am not very familiar with it yet. Currently I'm using this piece of code to send it : `router.get('/sensors/get-sensors-data', function(req, res) { var db = req.db; var collection = db.get('sensorsCollection'); collection.find({}, {sort: {date: -1}, limit: 3000}, function(e, docs){ if (e) return next(e); res.send(docs); }); });` – Harijoe Mar 02 '15 at 16:10

1 Answers1

0

you may want to use Array.filter(), see https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

to compact your array by removing rows every n, create this

Array.prototype.oneOutOf= function(n) {
  return this.filter(function(element,index) { 
    return (index%n)==0
  });
}

and then try

JSON.parse(jsondata).oneOutOf(10);
PA.
  • 28,486
  • 9
  • 71
  • 95
  • Thanks for your answer, I have trouble using JSON.parse though. It doesn't seem to work well with the structure of my data file (see original post). In addition I need to keep the JSON format to send it to `flot.js`. Could you complete your answer accordingly ? – Harijoe Mar 02 '15 at 16:34
  • accept this answer and post another question specific to your problem about JSON parser – PA. Mar 02 '15 at 16:46