1

I have an object array using the following model:

export interface Data { id: number qt: number date: string }

I use Akita's store and I would like to filter my object table so that I only have the results for the last 30 days.

    qtLastMonth(): Observable<Data[]> {
    return this.selectAll().pipe(
        map((allDatas) => allDatas.filter(data => 
            moment(data.date).format('DD-MM-YYYY') > 
            moment().subtract(30, "days").format('DD-MM-YYYY'))
        ))
}

However I get an output of an object table with dates later than the last 30 days ...

Maybe something is missing?

Boubou
  • 11
  • 1

1 Answers1

0

As @Anthony recommended you have to remove format as it would compare strings. Without it, predicate will be evaluated based on obj.valueOf() Unix Timestamp values.

const { of , operators: {
    map
  }
} = rxjs;


const selectAll = () => of (Array(10).fill(0).map((pr, index) => ({
  date: moment().subtract(25 + index, 'days').toDate()
})))

selectAll().pipe(
    map((allDatas) => {
      const monthAgo = moment().subtract(30, "days");
      return allDatas.filter(({
        date
      }) => moment(date) > monthAgo)
      // }) => moment(date).valueOf() > monthAgo.valueOf()) // equivalent
    }))
  .subscribe(result => {
    console.log(result)
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.5/rxjs.umd.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script>
Józef Podlecki
  • 10,453
  • 5
  • 24
  • 50