I am storing files into formData
like such:
formData.append("images[]", file)
formData.append("images[]", file_2)
formData.append("images[]", file_3)
I know it's possible to delete the files using formData.delete()
, but in this case, since all of the keys are the same, how do I specifically delete the object where the value is for example, file_2
?
Not sure if this is the optimal way. But you can get all values using getAll
. Then filter and append once again. Demo.
const form = new FormData
form.append('names[]', 'Chris')
form.append('names[]', 'Bob')
form.append('names[]', 'John')
console.log(Array.from(form.entries()))
const names = form.getAll('names[]')
form.delete('names[]')
names
.filter(name => name !== 'Bob')
.forEach(name => form.append('names[]', name))
console.log(Array.from(form.entries()))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With