I have example data like this
let datas = [
{ no : '1', list: [ {val: '1'}, {val: '3'},{val: '4'}] },
{ no : '2', list: [ {valNew: '11'}, {valNew: '13'},{valNew: '14'}] }
]
his expectations are like this
let datas = [
{ no : '1', list: [ {val: '1'}, { val: '2'}, { val: '3'},{val: '4'}] },
{ no : '2', list: [ {valNew: '11'}, {valNew: '13'},{valNew: '14'}] }
]
I've tried like this
const handleDuplicate = (index, idx, item) => {
let data = [...datas.listInvoice]
data[index] = {
...data[index] = {
...data[index],
invoices: [...data[index].invoices.slice(idx+1, 0, item)]
}
}
setDatas(prevState => {
return {
...prevState,
listInvoice: data
}
})
}
I want to add an object to an array with a certain position, but it always fails and the array inside invoices becomes [], what is the solution for this case?