我正在发布到API,并且如果没有在-1
表单字段中输入任何值,我想发送一个空数组。
我尝试使用filter方法,但是当我发出发布请求时,它会发送一个带有空对象的数组,即pricing
我的状态是:
[{}]
出票等于1时,将不会为title: "",
ticketing: "", // is equal to 0 (paid) or 1 (free)
pricing: [
{
price: "",
currency: "",
ticketLabel: ""
}
],
,price
和currency
输入任何值。
这是我的提交功能:
ticketLabel
我将其发布为
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)
}
当前,该帖子发布为:
axios
.post(`https://www.diaspora100.com/api/v1/public/events/`, {
info,
})
我希望它发布为:
price: [{}]
0: {}
感谢您的帮助!
答案 0 :(得分:2)
票务等于1时,不会输入价格值, 货币和ticketLabel
也许您想要这样的东西
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
}
] : []
}
答案 1 :(得分:0)
在价格的过滤器部分中,您可以尝试
<price-array>.filter(token => token.price.length > 0)