I would like to restructure a pain object to a 2D array object.
Originally, I have something like:
{
"education_histories.0.school":[
"The education_histories.0.school field is required."
],
"education_histories.0.degree":[
"The education_histories.0.degree field is required."
],
"education_histories.1.school":[
"The education_histories.1.school field is required."
],
"education_histories.1.degree":[
"The education_histories.1.degree field is required."
],
}
I would like to restructure it to be like:
[
{
"school":[
"The education_histories.0.school field is required."
],
"degree":[
"The education_histories.0.degree field is required."
]
},
{
"school":[
"The education_histories.1.school field is required."
],
"degree":[
"The education_histories.1.degree field is required."
]
}
]
Currently, I've been trying to do something like:
let validationErrors = []
$.each(this.errors, (key, value) => {
let splitKey = key.split('.')
validationErrors[splitKey[1]] = { [splitKey[2]]: value }
})
Of course, this won't work because It keeps overriding until the last round. The output would be like:
[
{
"degree":[
"The education_histories.0.degree field is required."
]
},
{
"degree":[
"The education_histories.1.degree field is required."
]
}
]
I wish I could do something like
let validationErrors = []
$.each(this.errors, (key, value) => {
let splitKey = key.split('.')
validationErrors[splitKey[1]][splitKey[2]] = value
})
But this won't work either. It says "TypeError: Cannot set property 'school' of undefined"
Any help would be really appreciated.