有没有人有办法在通过url之前删除id?反应 Axios

Does anyone have a way to delete id before passing through url? React Axios

有没有人有办法在通过url之前删除id? 我想删除 addedItems 数组中的 ID。

只删除id。不干扰里面的值

 onFinish = async(values) => {
    const { addedItems, total } = this.state;
    values.re_total = total;
    const { data, result } = this.props.loginReducer;
    await addedItems.forEach((doc) => {
      doc.car_number = values.cus_car_number;
      doc.reference = values.re_reference;
      doc.detail = values.re_detail;
      doc.adName = result.data.u_fname;
      doc.total = doc.price * doc.quantity;
    });
    delete addedItems.id;
    console.log(addedItems)
    await httpClient
            .post(`http://localhost:8085/api/v1/revenue/revenue`,{addedItems})
            .then((res) => {
              console.log(res);
            })
            .catch((error) => {
              console.log("Error :", error);
            })
    message.success({ content: 'บันทึกรายรับเรียบร้อย!', duration: 2, style: {
      marginTop: '5vh',
    }} ,100);
    await this.props.history.goBack();
  };

我假设 id 在 addedItems 数组的每一项中。而不是直接将其添加到数组中 (addedItems.id = 'bad')。

我从使用 Array#forEach 切换到 Array#map 以便它创建一个新数组。然后我将对象解构与其余运算符 (...) 一起使用。所以我可以从 doc 对象中提取 id 并同时制作 doc 的浅表副本。浅拷贝很重要,所以我们不会无意中修改状态值。

这实际上是您代码中最大的问题 - 它无意中修改了 this.state.addedItems,因为您在更改文档时没有先进行浅拷贝。

至于代码:用这个替换 await addedItems.forEach()。你实际上并不需要在那里等待,因为 Array#forEach 没有 return 任何东西,更不用说承诺了。

const mappedItems = addedItems.map(({id,...doc})=>{
      doc.car_number = values.cus_car_number;
      doc.reference = values.re_reference;
      doc.detail = values.re_detail;
      doc.adName = result.data.u_fname;
  return doc;
})

然后使用{addedItems: mappedItems}作为httpClient.post的正文。这样您就可以使用新数组而不是从状态中提取的旧数组。