I need to add year, month, day for my object. This is my current string array
["2021-11-01","2021-11-02"]
I need to convert this as below
[{ year: 2021, month: 11, day: 01 }, { year: 2021, month: 11, day: 02 }]
How i do this conversion. thank you
Advertisement
Answer
Just split()
the date into components, map()
the strings to numbers using parseInt()
, and create an object:
const data = ["2021-11-01","2021-11-02"]; const result = data.map(d => { const [year, month, day] = d.split('-').map(v => parseInt(v)); return {year, month, day}; }); console.log(result);