Skip to content

javascript object modification add key for the object [closed]

I need to add year, month, day for my object. This is my current string array


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


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 = => {
  const [year, month, day] = d.split('-').map(v => parseInt(v));
  return {year, month, day};