0

我向你解释我的问题,我有一个包含几个对象的表

在这个表格下

 {id:1,title:"Campus (Pinte)", desc:"Pression - Bière Blonde - 4° Alc", detail:"Une bière blonde légère qui saura vous désaltérer comme il se doit", qty:"50 cl", img:Campus, price: 5, ctg:1, },

我目前正在像这样发布我的整个表格

postbackend = () =>{
 const config = {

method: "POST",
headers: {
  "Content-Type": "application/json",
},
 body: JSON.stringify({...this.state, items:this.props.items}),
};

const url = entrypoint + "/alluserpls"; 
fetch(url, config)
.then(res => res.json())
.then(res => {
  if (res.error) {
    alert(res.error);
  } else {
    alert(`ajouté avec l'ID ${res}!`);
  }
}).catch(e => {
  console.error(e);

}).finally(()=>this.setState({ redirect: true }));
}

我只想发布我的桌子上没有总计的任何对象

例如我只想恢复标题数量和价格

你有一个想法怎么做。? 谢谢内夫

4

2 回答 2

1

作为this.props.items一个数组,您需要映射数组以创建一个新数组,其中包含仅具有所需字段的对象。

const newItems = this.props.items.map((item) => {
    const { title, qty, price } = item;
    return {
        title,
        qty,
        price
    };
});

然后使用你喜欢的变量:

method: "POST",
headers: {
  "Content-Type": "application/json",
},
 body: JSON.stringify({...this.state, items: newItems }),
};
于 2020-01-20T10:03:38.910 回答
0
    postbackend = () =>{
const { title, qty, price } = this.props.items;
const config = {

method: "POST",
headers: {
  "Content-Type": "application/json",
},
body: JSON.stringify({...this.state, items: { title, qty, price } }),
 };

 const url = entrypoint + "/alluserpls"; 
  fetch(url, config)
.then(res => res.json())
.then(res => {
  if (res.error) {
    alert(res.error);
  } else {
    alert(`film ajouté avec l'ID ${res}!`);
  }
}).catch(e => {
  console.error(e);

}).finally(()=>this.setState({ redirect: true }));
   }
于 2020-01-20T10:59:26.383 回答