(可选)在对象中添加键

时间:2019-03-23 18:10:07

标签: javascript

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

考虑我有以下功能

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); 
}

现在此函数被假定为可重用,从前端有两个实例可以调用它。

  1. 在第一个实例中,我将发送一些linkURL
  2. 在第二种情况下,我将传递null作为linkURL

现在,我想要的是:如果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只是请求承诺

3 个答案:

答案 0 :(得分:4)

仅不要在对象文字中添加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); 
}

答案 1 :(得分:1)

您可以使用对象传播语法:

formData: {
    await: "true",
    source: {
        value: filePath.data,
        options: {
            filename: imageName
        }
    },
    message:longText,
    ...(linkURL && {link: linkURL})
}

(linkURL && {link: linkURL})是一个表达式,可以变成null(当linkURL具有您所描述的值时)或{link: linkURL}

在第一种情况下,...null不起作用,因为null没有属性。

在第二种情况下,{link: linkURL}的浅表副本将合并到外部对象中。

答案 2 :(得分:1)

在JS undefined语句中使用三元语句:

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",
        link: linkUrl ? linkUrl : undefined, // If linkUrl, add it. Otherwise, undefined.
        source: {
          value: filePath.data,
          options: {
            filename: imageName
          }
        },
        message:longText,
      }
    };
    return requestP(uploadPayload); 
}