How to remove an object by index from array of objects in array of objects.
I want to remove the first object from the array with a variable value in the object id "A1"
let data =
[
{ id: 'A1',
value: [ { uri: 'B45AA03A05B7.jpg', type: 'image/jpeg' },
{ uri: '30A42FBC1146.jpg', type: 'image/jpeg' } ]
},
{ id: 'A2',
value: [ { uri: 'G455HFG2FF56.jpg', type: 'image/jpeg' },
{ uri: 'TY6DFG7RTGF.jpg', type: 'image/jpeg' } ]
}
]
required output
let data =
[
{ id: 'A1',
value: [ { uri: '30A42FBC1146.jpg', type: 'image/jpeg' } ]
},
{ id: 'A2',
value: [ { uri: 'G455HFG2FF56.jpg', type: 'image/jpeg' },
{ uri: 'TY6DFG7RTGF.jpg', type: 'image/jpeg' } ]
}
]
i tried this
const array1 = data.filter((item) => item.id == deleteId)
if (delIndex > -1) {
array1[0].value.splice(delIndex, 1);
}
delIndexis 0, it deletes the first, and 1 deletes the second ... how isdelIndexset?dataorarray1in a state? If so you probably don't want to usesplice()since a state should not be mutated, only re-assigned.