I need to write a function which will filter out relevant data from a csv file and return an array containing the data.
The csv file contains time as one of the columns and I want to filter out the rows within specified start and end time as provided in function arguments. I have written the follwing function filter_data
(please also note that this uses d3.js):
<code>
function filter_data(start_time, end_time) {
var formatDate = d3.time.format("%H:%M"),
parseDate = formatDate.parse;
var data1 = [];
d3.csv("data.txt", function(error, data) {
data.forEach(function(d) {
if(parseDate(d.time) >= parseDate(start_time) && parseDate(d.time) < parseDate(end_time))
data1.push(d);
});
return data1;
});
}
</code>
The idea behind this was to build reusable code for getting data. But the above function always returns undefined
. I have deliberately placed the return value inside the callback function as I know this will be an asynchronous call (I also trie placing return statement at the end, but it returns empty array!).