0

Say I have an array of objects like on the picture:

Array of objects

Each object contains property ShipperPickupDate with a string value made from date object by calling toLocaleTimeString('en-US') method.

What I need is to filter this array of objects by the date range. Say I want to get new array containing objects filtered by shipperPickupDate in range from 2/10/2020 to 3/18/2020. Is there any method or suggestions?

Sorry for not providing any code. I'm still in searching of a proper way to do this so any suggestions are highly appreciated! Thank you!

8
  • 1
    Array.prototype.filter() Commented Mar 10, 2020 at 16:26
  • you will need to use Array.prototype.map() to create your date object from the string values. Then pipe this through Array.prototype.filter() to get the date range you need. Commented Mar 10, 2020 at 16:30
  • let newArr = this.loadsFiltered.forEach(el => { return ( el.shipperPickupDate >= this.date1 && el.shipperPickupDate <= this.date2 ); }); Would that work? Commented Mar 10, 2020 at 16:33
  • 1
    You should use a library like momentjs to deal with date, it helps a lot and it has a function isBetween that you can use. Commented Mar 10, 2020 at 16:34
  • 1
    @Madcap another tips I can give you, do not forget that toLocaleTimeString gives you a date based on locale timezone, could be a good idea to get your dates "rawly" to get UTC time too. Commented Mar 10, 2020 at 16:37

1 Answer 1

0

You can use Array.prototype.filter(). Just compare date in callback of filter function then it will return filtered array.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.