axios 要删除列表中对象数据的id,我应该做什么?

g6ll5ycj  于 2023-03-18  发布在  iOS
关注(0)|答案(1)|浏览(200)

我先发送数据,然后就没有问题了,但是当我想更新的时候,我可以删除根对象的id,但是我不能删除列表中对象的id,有什么想法吗?

const handleSubmit = async (values: any) => {
    setIsLoading(true);
    if (!id) {
      usersService
        .create(values)
        .then((response) => {
          saved(response.data._id);
        })
        .catch((error) => {
          message.error(`${t("error")} ${error.response.data.message}`);
        })
        .finally(() => {
          setIsLoading(false);
        });
    }
    if (id) {
      const formData = { ...result, ...values };
      console.log(formData);
      delete formData._id;
      delete formData.createdAt;
      delete formData.updatedAt;
      delete formData.List.map((x : any) => x._id) // this didn't work
      delete formData.enrolledCourses;
      delete formData.isProfileCompleted;
      usersService
        .update(id, formData)
        .then(() => {
          saved();
          rowHighlight(id);
        })
        .catch((error) => {
          message.error(`${t("error")} ${error.response.data.message}`);
        })
        .finally(() => {
          setIsLoading(false);
        });
    }
  };

我尝试了map函数和forEach函数。我尝试删除返回值 函数的内部和外部都找过了,但没找到

efzxgjgh

efzxgjgh1#

您需要Map每个列表项并删除它们的id属性,如下所示:

formData.List.map((x : any) => {
   const newX = {...x};
   delete newX._id;
   return newX
})

相关问题