Reputation: 223
Hi I would like to filter my data between date ranges.
{
"websiteId": "4f8b36d00000000000000001",
"date": "2019-04-01T00:00:00.000Z",
"chats": 121,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000002",
"date": "2019-04-01T00:00:00.000Z",
"chats": 13,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000003",
"date": "2019-04-01T00:00:00.000Z",
"chats": 232,
"missedChats": 9
},
{
"websiteId": "4f8b36d00000000000000004",
"date": "2019-04-01T00:00:00.000Z",
"chats": 9,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000005",
"date": "2019-04-03T00:00:00.000Z",
"chats": 0,
"missedChats": 5
},
{
"websiteId": "4f8b36d00000000000000006",
"date": "2019-04-03T00:00:00.000Z",
"chats": 10,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000007",
"date": "2019-04-01T00:00:00.000Z",
"chats": 100,
"missedChats": 1
},
{
"websiteId": "4f8b36d00000000000000008",
"date": "2019-04-01T00:00:00.000Z",
"chats": 0,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000007",
"date": "2019-04-02T00:00:00.000Z",
"chats": 0,
"missedChats": 1
},
{
"websiteId": "4f8b36d00000000000000006",
"date": "2019-04-02T00:00:00.000Z",
"chats": 100,
"missedChats": 50
}
My current implementation
function getData(start, end) {
if (start == null && end == null) {
fetch(url)
.then((res) => res.json())
.then((data) => {
console.log(
Object.values(
data.reduce((acc, el) => {
if (!(el.websiteId in acc)) {
acc[el.websiteId] = el;
} else {
acc[el.websiteId].chats += el.chats;
acc[el.websiteId].missedChats += el.missedChats;
}
return acc;
}, {})
)
);
});
} else {
var start = new Date('2019-04-01T00:00:00.000Z').getTime();
var end = new Date('2019-04-05T00:00:00.000Z').getTime();
result = data.filter((d) => {
var time = new Date(d.date).getTime();
return start < time && time < end;
});
console.log(result);
}
}
Expected Output
when I run getData(startDate(2019-4-01), endDate(2019-4-02)) I should be able to get the data between those date range
{
"websiteId": "4f8b36d00000000000000007",
"date": "2019-04-02T00:00:00.000Z",
"chats": 100,
"missedChats": 2
},
{
"websiteId": "4f8b36d00000000000000006",
"date": "2019-04-02T00:00:00.000Z",
"chats": 110,
"missedChats": 50
}
I would like to get data between the range of date when I run the function getDate(startDate, endDate)
Upvotes: 0
Views: 388
Reputation: 16448
You should move the if condition into the .then
block:
const data = [{
"websiteId": "4f8b36d00000000000000001",
"date": "2019-04-01T00:00:00.000Z",
"chats": 121,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000002",
"date": "2019-04-01T00:00:00.000Z",
"chats": 13,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000003",
"date": "2019-04-01T00:00:00.000Z",
"chats": 232,
"missedChats": 9
},
{
"websiteId": "4f8b36d00000000000000004",
"date": "2019-04-01T00:00:00.000Z",
"chats": 9,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000005",
"date": "2019-04-03T00:00:00.000Z",
"chats": 0,
"missedChats": 5
},
{
"websiteId": "4f8b36d00000000000000006",
"date": "2019-04-03T00:00:00.000Z",
"chats": 10,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000007",
"date": "2019-04-01T00:00:00.000Z",
"chats": 100,
"missedChats": 1
},
{
"websiteId": "4f8b36d00000000000000008",
"date": "2019-04-01T00:00:00.000Z",
"chats": 0,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000007",
"date": "2019-04-02T00:00:00.000Z",
"chats": 0,
"missedChats": 1
},
{
"websiteId": "4f8b36d00000000000000006",
"date": "2019-04-02T00:00:00.000Z",
"chats": 100,
"missedChats": 50
}];
function getData(start, end) {
/*fetch(url)
.then((res) => res.json())
.then((data) => {*/
const summedData =
Object.values(
data.reduce((acc, el) => {
if (!(el.websiteId in acc)) {
acc[el.websiteId] = el;
} else {
acc[el.websiteId].chats += el.chats;
acc[el.websiteId].missedChats += el.missedChats;
}
return acc;
}, {}));
if (start && end) {
result = summedData.filter((d) => {
const time = new Date(d.date).getTime();
return start < time && time < end;
});
console.log(result);
} else {
console.log(summedData);
}
/*});*/
}
const start = new Date('2019-04-01T00:00:00.000Z').getTime();
const end = new Date('2019-04-05T00:00:00.000Z').getTime();
getData(start, end);
getData();
Upvotes: 1
Reputation: 35603
Given your data, your filter
method is correct, you should convert your date strings into a Date
and compare with getTime
.
const data = [{
"websiteId": "4f8b36d00000000000000001",
"date": "2019-04-01T00:00:00.000Z",
"chats": 121,
"missedChats": 0
}, {
"websiteId": "4f8b36d00000000000000002",
"date": "2019-04-01T00:00:00.000Z",
"chats": 13,
"missedChats": 0
}, {
"websiteId": "4f8b36d00000000000000003",
"date": "2019-04-01T00:00:00.000Z",
"chats": 232,
"missedChats": 9
}, {
"websiteId": "4f8b36d00000000000000004",
"date": "2019-04-01T00:00:00.000Z",
"chats": 9,
"missedChats": 0
}, {
"websiteId": "4f8b36d00000000000000005",
"date": "2019-04-03T00:00:00.000Z",
"chats": 0,
"missedChats": 5
}, {
"websiteId": "4f8b36d00000000000000006",
"date": "2019-04-03T00:00:00.000Z",
"chats": 10,
"missedChats": 0
}, {
"websiteId": "4f8b36d00000000000000007",
"date": "2019-04-01T00:00:00.000Z",
"chats": 100,
"missedChats": 1
}, {
"websiteId": "4f8b36d00000000000000008",
"date": "2019-04-01T00:00:00.000Z",
"chats": 0,
"missedChats": 0
}, {
"websiteId": "4f8b36d00000000000000007",
"date": "2019-04-02T00:00:00.000Z",
"chats": 0,
"missedChats": 1
}, {
"websiteId": "4f8b36d00000000000000006",
"date": "2019-04-02T00:00:00.000Z",
"chats": 100,
"missedChats": 50
}];
function getData(start, end) {
const startTime = new Date(start).getTime();
const endTime = new Date(end).getTime();
return data.filter(item => {
const itemTime = new Date(item.date).getTime();
return itemTime >= startTime && itemTime <= endTime;
})
}
console.log(getData('2019-04-01', '2019-04-02'))
Upvotes: 1