showing results for - "javascript search json array for date"
Joyce
14 Apr 2018
1var startDate = new Date("2015-08-04");
2        var endDate = new Date("2015-08-12");
3
4        var resultProductData = product_data.filter(function (a) {
5            var hitDates = a.ProductHits || {};
6            // extract all date strings
7            hitDates = Object.keys(hitDates);
8            // improvement: use some. this is an improment because .map()
9            // and .filter() are walking through all elements.
10            // .some() stops this process if one item is found that returns true in the callback function and returns true for the whole expression
11            hitDateMatchExists = hitDates.some(function(dateStr) {
12                var date = new Date(dateStr);
13                return date >= startDate && date <= endDate
14            });
15            return hitDateMatchExists;
16        });
17        console.log(resultProductData);