16

I am trying to filter a data-array of a LineChart by the from-date / to-date input of a user in TypeScript for my Angular App. The data array has the following structure:

var multi = [
    {
      "name": "test1",
      "series": [
        {
            "date": new Date("2018-01-01T01:10:00Z"),
            "value": 44
        },...
      ]
    },
    {
      "name": "test2",
      "series": [
        {
            "date": new Date("2018-01-01T01:10:00Z"),
            "value": 38
          },...
      ]
    },
    {
      "name": "test3",
      "series": [
        {
            "date": new Date("2018-01-01T01:10:00Z"),
            "value": 33
          },...
      ]
    }
  ];

I now want to filter the items of the array by the criteria that the date inside is after a 'fromDate' and before a 'toDate'. I tried the following:

obj.forEach(data => {
            console.log(data.name);
            data.series = data.series.filter((item: any) => {
                item.date.getTime() >= fromDate.getTime() &&
                item.date.getTime() <= toDate.getTime();
            });
        });

the obj[] array has an empty obj[i].series array afterwards. Can anybody help me here? The iteration seems to be right since debugging gave me all the dates, also the true/False statements from the date comparing was right as well.

Thanks in advance

0

3 Answers 3

45

You need to return the compairing value, either explicit

data.series = data.series.filter((item: any) => {
    return item.date.getTime() >= fromDate.getTime() &&
           item.date.getTime() <= toDate.getTime();
});

or without the brackets, implicit.

data.series = data.series.filter((item: any) =>
    item.date.getTime() >= fromDate.getTime() && item.date.getTime() <= toDate.getTime()
);
Sign up to request clarification or add additional context in comments.

1 Comment

To add to this, there is a difference between using brackets and not using them on arrow functions. When you use brackets, you're expected to write a full body of a function - including the return statement. When NOT using brackets, you're expected to write only the in-lined statement that is supposed to be returned by the arrow function. So, to solve this problem, either remove the brackets or include the return keyword.
6

I had an issue with this when the end date was the same as the start date to solve this issue I had to set the time on the end date to 23.59

const start =  new Date().getTime()
const end=new Date()
end.setHours(23,59,59,999)
end.getTime()

return items.filter(item => {
   let date = new Date(item.created_at).getTime();
   return date >= start && date <= end;
}

Comments

4
let start = new Date(this.min);
let end   = new Date(this.max);

return items.filter(item => {
   let date = new Date(item.created_at);
   return date >= start && date <= end;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.