Amadou Tafsir Diallo
Amadou Tafsir Diallo

Reputation: 47

How can I use lodash to filter value between two date?

created_at is the value I Want to compare with the beginDate and the endDate.

var newARRA = chain(this.props.transferts.transfertAccount)
.filter({ 'officeSender':
      this.props.users.user.office,officeReceiver:this.state.selectedOffice,
	 'created_at':(this.state.beginDate,this.state.endDate)})
				
.groupBy("currency")
.map((v, k) => ({
     currency: k,
	 amount: sumBy(v, 'fee')
	}))
.value();

Upvotes: 2

Views: 4653

Answers (3)

PRogalla
PRogalla

Reputation: 236

I would prefer using a date library. > date-fns could do this for you

var isWithinRange = require('date-fns/is_within_range')
isWithinRange(
   new Date(2014, 0, 3), new Date(2014, 0, 1), new Date(2014, 0, 7)
)//=> true

Upvotes: 1

Ori Drori
Ori Drori

Reputation: 191976

You'll need to use the filter's predicate function. Example (not tested):

var newARRA = chain(this.props.transferts.transfertAccount)
  .filter(({ officeSender, officeReceiver, created_at }) => {
    return _.eq(officeSender, this.props.users.user.office) &&
           _.eq(officeReceiver, this.state.selectedOffice) &&
           _.gte(created_at, this.state.beginDate) &&
           _.lte(created_at, this.state.endDate) &&
  })
  .groupBy("currency")
  .map((v, k) => ({
    currency: k,
    amount: sumBy(v, 'fee')
  }))
  .value();

Upvotes: 3

rrd
rrd

Reputation: 5957

You could use the moment library to achieve this for you, assuming that the two values are moment like dates:

'created_at': moment().isBetween(this.state.beginDate, this.state.endDate)

This does assume that the dates are moment-like and don't require any pre-processing by the moment library.

Upvotes: 0

Related Questions