-1

I have an array like so where each 'category' value is a string in the format "YYYY-MM-DD':

let originalData = [
    {
        "category": "2017-07-24",
        "total": 1,
        "Col 1": 200
    },
    {
        "category": "2018-07-10",
        "total": 1,
        "Col 1": 100
    },
    {
        "category": "2018-11-12",
        "total": 1,
        "Col 1": 101
    },
    {
        "category": "2019-08-11",
        "total": 1,
        "Col 1": 153
    },
    {
        "category": "2019-09-11",
        "total": 1,
        "Col 1": 198
    }
]

How can I convert it so that the "category value" is as follows for each object?

let desiredResult = [
    {
        "category": new Date(2017, 7, 24).getTime(),
        "total": 1,
        "Col 1": 200
    },
    {
        "category": new Date(2018, 7, 10).getTime(),
        "total": 1,
        "Col 1": 100
    },
    {
        "category": new Date(2018, 11, 12).getTime(),
        "total": 1,
        "Col 1": 101
    },
    {
        "category": new Date(2019, 8, 11).getTime(),
        "total": 1,
        "Col 1": 153
    },
    {
        "category": new Date(2019, 9, 11).getTime(),
        "total": 1,
        "Col 1": 198
    }
]

Bear in mind that the values in the original "category" key has the months and dates listed such that there is a 0 in singular dates: "2017-07-24" i.e. there's a 0 before 7 in the MM section. However, new Date(2017, 7, 24) does not work if you do something like new Date(2017, 07, 24), it has to be singular.

I know I can do something like

this.originalData.forEach( item => {
    // somehow retrieve the specific sections in that item's category
    item.category = new Date(..??..).getTime()
})

However, I'm unsure as to how to filter it so that it we get new Date(YYYY-MM-DD) WHILE removing any 0s before any month or day with a singular number e.g.

2017-08-08 would be new Date(2017, 8, 8).getTime() 2013-09-22 would be new Date(2013, 9, 22).getTime() etc.

phuzi
  • 12,078
  • 3
  • 26
  • 50
Beginner_Hello
  • 357
  • 6
  • 19
  • First parse the dates using [strptime](https://www.programiz.com/python-programming/datetime/strptime), then turn them into formated dates with [strftime](https://www.programiz.com/python-programming/datetime/strftime). Surely these exist in JS as well. If you can play around in python first using these examples it may make it easier to implement in JS. – Yarin_007 Dec 08 '22 at 12:35
  • You do NOT want to change `2017-07-24` to `new Date(2017,7,24)` since that will be a month later. – mplungjan Dec 08 '22 at 12:47
  • [Date.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) takes yyyy-mm-dd, so all that is needed is `item.category = Date.parse(item.category)` – mplungjan Dec 08 '22 at 12:59
  • Does this answer your question? [Parsing a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/parsing-a-string-to-a-date-in-javascript) – Don't Panic Dec 08 '22 at 21:06

2 Answers2

1

You can do:

const originalData = [{category: '2017-07-24',total: 1,'Col 1': 200,},{category: '2018-07-10',total: 1,'Col 1': 100,},{category: '2018-11-12',total: 1,'Col 1': 101,},{category: '2019-08-11',total: 1,'Col 1': 153,},{category: '2019-09-11',total: 1,'Col 1': 198,},]

const result = originalData.map((item) => {
  const [year, month, day] = item.category.split('-')
  return {
    ...item,
    category: new Date(+year, +month - 1, +day).getTime(),
  }
})

console.log(result)
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
1

const originalData = [{category: '2017-07-24',total: 1,'Col 1': 200,},{category: '2018-07-10',total: 1,'Col 1': 100,},{category: '2018-11-12',total: 1,'Col 1': 101,},{category: '2019-08-11',total: 1,'Col 1': 153,},{category: '2019-09-11',total: 1,'Col 1': 198,},]

const result = originalData.map((x) => {
  return {...x, category: new Date(x.category).getTime()};
});

console.log(result);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Eddy Lin
  • 513
  • 2
  • 6