I am posting to an API, and if no values are entered in the pricing
form fields, I want to send an empty array.
I have tried using the filter method but when I make the post request it sends an array with an empty object ie [{}]
My state is:
title: "",
ticketing: "", // is equal to 0 (paid) or 1 (free)
pricing: [
{
price: "",
currency: "",
ticketLabel: ""
}
],
When ticketing is equal to 1, no values will be inputted for price
, currency
and ticketLabel
.
This is my submit function:
handleSubmit = async (event) => {
event.preventDefault();
const info = {
title: this.state.title,
ticketing: this.state.ticketing,
price: [
{
currency: this.state.currency,
price: this.state.price,
label: this.state.ticketLabel
}
].filter(token => Object.keys(token).length != 0)
}
And I post it as
axios
.post(`https://www.diaspora100.com/api/v1/public/events/`, {
info,
})
Currently, this posts as:
price: [{}]
0: {}
I would like it to post as:
price: []
Thanks for any help!
When ticketing is equal to 1, no values will be inputted for price, currency and ticketLabel
maybe you want something like this
const info = {
title: this.state.title,
ticketing: this.state.ticketing,
price: this.state.ticketing !== 1 ? [
{
currency: this.state.currency,
price: this.state.price,
label: this.state.ticketLabel
}
] : []
}
在价格的过滤器部分你可以试试这个
<price-array>.filter(token => token.price.length > 0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.