const list = []
const obj = {
name: '',
mobile: ''
}
_.forEach(errors, (value, key) => {
// eslint-disable-next-line no-debugger
// debugger
const field = key.split('.')[2]
const index = key.split('.')[1]
obj[field] = value[0]
list.splice(index, 1, obj)
console.log(obj)
})
This code snippet showcases the current progress of my work. Following this, you will find a screenshot displaying the logs for each obj
variable.
https://i.sstatic.net/BALaM.png
The main objective is to incorporate that object into the specified index
within the list
variable.
However, upon completion of the loop, what I end up with is illustrated in the subsequent image:
https://i.sstatic.net/jvZIy.png
It appears that the last item in the loop overrides all the content in the list
array.
For reference, here are the available indexes
:
https://i.sstatic.net/q7mXp.png
In essence, I am striving to have the obj
inserted at a specific index
within the existing list
array.
EDIT
The structure of the errors
variable is demonstrated below:
https://i.sstatic.net/iWE2h.png
My aim is to transform it into something similar to the following:
list = [
{ name: 'name error message here', mobile: 'error message here' },
{ name: 'name error message here', mobile: 'error message here' },
{ name: 'name error message here', mobile: 'error message here' },
{ name: 'name error message here', mobile: 'error message here' }
]