我想将疼痛对象重组为二维数组对象。
最初,我有类似的东西:
{
"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."
],
}
我想将其重组为:
[
{
"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."
]
}
]
目前,我一直在尝试执行以下操作:
let validationErrors = []
$.each(this.errors, (key, value) => {
let splitKey = key.split('.')
validationErrors[splitKey[1]] = { [splitKey[2]]: value }
})
当然,这行不通,因为它一直覆盖到最后一轮。输出将是这样的:
[
{
"degree":[
"The education_histories.0.degree field is required."
]
},
{
"degree":[
"The education_histories.1.degree field is required."
]
}
]
我希望我能做类似的事情
let validationErrors = []
$.each(this.errors, (key, value) => {
let splitKey = key.split('.')
validationErrors[splitKey[1]][splitKey[2]] = value
})
但这也行不通。它说"TypeError: Cannot set property 'school' of undefined"
任何帮助将非常感激。
守着星空守着你
猛跑小猪
慕沐林林
相关分类