-1

I am working on a billing app using Angular. My items array returns as follows (1 item partly shown here):-

    items: [{
       _id: "5ee2298f59ce2e1747cb5539"
        createdAt: "2020-06-11T12:54:40.031Z"
        itemDescription: "White Bread of small size."
        itemGroup: {_id: "5ec7bab54794600d9f4cd24f", itemGroupName: "Breads White"}
        itemName: "Bread White - Small Slice"
        itemStdRate:
        0: {_id: "5eff39485c73781aa1b0b60f", stdRate: 20, applicableFrom: "2020-07-28T18:30:00.000Z"}
        1: {_id: "5f02d64ccc5e5f15bfb73f41", stdRate: 18, applicableFrom: "2020-07-06T07:43:58.446Z"}
        length: 2
        itemUnit: {_id: "5ec932fe54799a1e7bbe9cd5", itemUnitSymbol: "nos", itemUnitName: "Numbers"}
        updatedAt: "2020-07-06T13:02:01.369Z"
    }]

I want it to return the stdRate of the latest applicableFrom date till a given date from the subArray of itemStdRate.

  • "items" is an object, not an array. Please provide an accurately formed object/array/however the thing is actually returned, if you would like to receive help in filtering to the correct itemStdRate. – BryanOfEarth Jul 10 '20 at 15:07
  • Could you please share the code you tried for achieving the same? – Harmandeep Singh Kalsi Jul 10 '20 at 15:07
  • To achieve what you want, I think you have to change the structure of your data so that `itmStdRate` would be an array. You can then sort the array by the `applicableFrom` property and then compare with the input date – maswerdna Jul 10 '20 at 15:12
  • Your sample is not a `JSON` (try to parse it with any online JSON parser - you'll get an error). It's not an `Array`, it's not an `Object` (at least not in JavaScript). It is similar to a `JSON` or an `Object`, but it looks like you copied it from a source that doesn't respect formatting (like `console`) – muka.gergely Jul 10 '20 at 18:42
  • This is just a part of the object array.. – Winlight Solutions Jul 11 '20 at 14:16

1 Answers1

0

If we assume that your real data is something like this:

{
  items: [{
    _id: "5ee2298f59ce2e1747cb5539",
    createdAt: "2020-06-11T12:54:40.031Z",
    itemDescription: "White Bread of small size.",
    itemGroup: {
      _id: "5ec7bab54794600d9f4cd24f",
      itemGroupName: "Breads White"
    },
    itemName: "Bread White - Small Slice",
    itemStdRate: [{
        _id: "5eff39485c73781aa1b0b60f",
        stdRate: 20,
        applicableFrom: "2020-07-28T18:30:00.000Z"
      }, {
        _id: "5f02d64ccc5e5f15bfb73f41",
        stdRate: 18,
        applicableFrom: "2020-07-06T07:43:58.446Z"
      }
    ],
    length: 2,
    itemUnit: {
      _id:"5ec932fe54799a1e7bbe9cd5",
      itemUnitSymbol: "nos",
      itemUnitName: "Numbers"
    },
    updatedAt: "2020-07-06T13:02:01.369Z",
  }]
}

Then you can create the a filtering function like this:

const { items } = {
  items: [{
    _id: "5ee2298f59ce2e1747cb5539",
    createdAt: "2020-06-11T12:54:40.031Z",
    itemDescription: "White Bread of small size.",
    itemGroup: {
      _id: "5ec7bab54794600d9f4cd24f",
      itemGroupName: "Breads White"
    },
    itemName: "Bread White - Small Slice",
    itemStdRate: [{
      _id: "5eff39485c73781aa1b0b60f",
      stdRate: 20,
      applicableFrom: "2020-07-28T18:30:00.000Z"
    }, {
      _id: "5f02d64ccc5e5f15bfb73f41",
      stdRate: 18,
      applicableFrom: "2020-07-06T07:43:58.446Z"
    }],
    length: 2,
    itemUnit: {
      _id: "5ec932fe54799a1e7bbe9cd5",
      itemUnitSymbol: "nos",
      itemUnitName: "Numbers"
    },
    updatedAt: "2020-07-06T13:02:01.369Z",
  }]
}


/*** ORIGINAL ANSWER BEGIN ***/
// latest date code source: https://stackoverflow.com/questions/36577205/what-is-the-elegant-way-to-get-the-latest-date-from-array-of-objects-in-client-s
/*const getLatestDate = (itemStdRate) => {
  return Math.max.apply(null, itemStdRate.map(function(e) {
    return new Date(e.applicableFrom);
  }))
}

const latestStdRates = items.map(({
  itemStdRate
}) => {
  const latest = getLatestDate(itemStdRate)
  return itemStdRate.find(e => new Date(e.applicableFrom).getTime() === latest)
})*/
/*** ORIGINAL ANSWER END ***/

/*** NEW ANSWER BEGIN ***/
// all dates before a given date
const datesBefore = (items, dateBefore) => {
  return items.map(({ itemStdRate }) => {
    return itemStdRate.filter(({ applicableFrom }) => {
      return new Date(applicableFrom) <= new Date(dateBefore)
    })
  })
}

console.log('date now:', datesBefore(items, Date.now()))
console.log('2020-07-29:', datesBefore(items, new Date("2020-07-29")))
/*** NEW ANSWER END ***/
muka.gergely
  • 8,063
  • 2
  • 17
  • 34
  • Ok, Thanks, but that will just get me the rate of the maximum date. The data could have future dates as well. I want the maximum date but less than or equal to the given date say today. – Winlight Solutions Jul 11 '20 at 07:05
  • @WinlightSolutions if you want to get all items before a certain date, you just have to filter your items according to that date. The snippet is updated according to this. – muka.gergely Jul 13 '20 at 18:40