I have a Jquery script that pulling external data from a json file.
How can i filter result by date? Thanks
$.getJSON("Content/Chart/iron/Test2.json", function (data) {
var items = [];
$.each(data, function (key, val) {
items.push("<div id='" + key + "' class='list-group-item list-group-item-action'>" + val.date + " " + val.cars + "</div>");
});
$("<div/>", {
"class": "container list-group",
html: items.join("")
}).appendTo("#Secim");
});
This is my json file content
[
{
"date": "2009.10.14",
"cars": 1587,
"motorcycles": 650,
"bicycles": 121
},
{
"date": "2009.10.15",
"cars": 1567,
"motorcycles": 683,
"bicycles": 146
},
{
"date": "2009.10.16",
"cars": 1617,
"motorcycles": 691,
"bicycles": 138
},
{
"date": "2009.10.17",
"cars": 1630,
"motorcycles": 642,
"bicycles": 127
},
{
"date": "2009.10.18",
"cars": 1660,
"motorcycles": 699,
"bicycles": 105
},
{
"date": "2009.10.19",
"cars": 1683,
"motorcycles": 721,
"bicycles": 109
}
]
What I have tried:
Perhaps i found two solutions from internet (stackoverflow) but i dont know how can i use it
var filteredData = this.orders.filter(function(a){
aDate = new Date(a.fecha);
return aDate >= startDate && aDate <= endDate;
});
or this
var startDate = new Date("2015-08-04");
var endDate = new Date("2015-08-12");
var resultProductData = product_data.filter(function (a) {
var hitDates = a.ProductHits || {};
hitDates = Object.keys(hitDates);
hitDateMatchExists = hitDates.some(function(dateStr) {
var date = new Date(dateStr);
return date >= startDate && date <= endDate
});
return hitDateMatchExists;
});
console.log(resultProductData);