Upon receiving the following JSON file:
var data = [
{
"id":"1",
"yMonth":"201901",
},
{
"id":"2",
"yMonth":"201902",
},
{
"id":"3",
"yMonth":"201802",
},
{
"id":"4",
"yMonth":"202003",
}
]
The yMonth
property in my data contains a concatenation of year and month (201901 translates to year: 2019, month: 01). I am looking to split the yMonth
into two separate items for year and month, and sort them accordingly.
The desired output is as follows:
[
{
"id":"1",
"year":"2019",
"month":"01",
},
{
"id":"2",
"year":"2019",
"month":"02",
},
{
"id":"3",
"year":"2018",
"month":"02",
},
{
"id":"4",
"year":"2020",
"month":"03",
}
]
I am considering starting with the following approach:
data.forEach(item => {
item.yMonth.....
//Split string, potentially save in auxiliary variable, and create a new array with `year` and `month` items
})
Any guidance on how to achieve this would be greatly appreciated. I am currently struggling with this task.