我想有选择地在对象中添加一个键

考虑我有以下功能

const uploadFacebookPostWithPhoto = async (filePath, imageName, longText, linkURL, pageID, accessToken) => {

    const uploadPayload = {
      json: true,
      method: "POST",
      uri: `https://graph.facebook.com/${pageID}/photos`,
      headers: {
        Authorization: "Bearer " + accessToken
      },
      formData: {
        await: "true",
        source: {
          value: filePath.data,
          options: {
            filename: imageName
          }
        },
        message:longText,
        link: linkURL,
      }
    };
    return requestP(uploadPayload);
}

现在,此函数被假定为可重用,从前端开始,有两个实例将在其中调用。
  • 在我打电话的第一个实例中,我将发送一些linkURL
  • 在第二种情况下,我将只为linkURL传递null

  • 现在,我想要的是:如果linkURl不等于null,那么formData应该具有链接键/值对;如果linkURL为null,那么我的formData不应具有linkURL键/值。

    最简单的解决方案是创建if-else语句
    const uploadFacebookPostWithPhoto = async (filePath, imageName, longText, linkURL, pageID, accessToken) => {
    if (linkURL) {
            const uploadPayload = {
              json: true,
              method: "POST",
              uri: `https://graph.facebook.com/${pageID}/photos`,
              headers: {
                Authorization: "Bearer " + accessToken
              },
              formData: {
                await: "true",
                source: {
                  value: filePath.data,
                  options: {
                    filename: imageName
                  }
                },
                message:longText,
                link: linkURL,
              }
            };
            return requestP(uploadPayload);
    } else {
     const uploadPayload = {
              json: true,
              method: "POST",
              uri: `https://graph.facebook.com/${pageID}/photos`,
              headers: {
                Authorization: "Bearer " + accessToken
              },
              formData: {
                await: "true",
                source: {
                  value: filePath.data,
                  options: {
                    filename: imageName
                  }
                },
                message:longText
              }
            };
            return requestP(uploadPayload);
    }
        }
    

    但我一直在寻找更好的解决方案,有人可以提出替代解决方案(除了创建单独的功能之外)吗?这个问题的目的是提高我的代码质量

    注意:requestP只是请求 promise

    最佳答案

    只是不要在对象文字中添加link键。以后根据条件添加

    const uploadFacebookPostWithPhoto = async (filePath, imageName, longText, linkURL, pageID, accessToken) => {
    
        const uploadPayload = {
          json: true,
          method: "POST",
          uri: `https://graph.facebook.com/${pageID}/photos`,
          headers: {
            Authorization: "Bearer " + accessToken
          },
          formData: {
            await: "true",
            source: {
              value: filePath.data,
              options: {
                filename: imageName
              }
            },
            message:longText,
          }
        };
        if(linkUrl) uploadPayLoad.formData.link = linkURL,
        return requestP(uploadPayload);
    }
    

    09-12 20:57