JavaScript Optionally add a key in object

I want to optionally add a key in object

consider I have a following function

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

now this function is suppose to be reusable meaning, from the frontend there are two instance from where I would be calling it.

  1. In the first instance from where I am calling, I will send some linkURL
  2. In the second instance, I will just pass null for linkURL

Now, What I want is If the linkURl is not equal to null my formData should have link key/value pair and if the linkURL is null, my formData shouldn't have linkURL key/Value.

The easiest solution would be to create if-else statement

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

but I was looking for a better solution, Can someone suggest an alternate solution (besides creating a separate function)? Purpose of this question is to improve my code quality

Note: requestP is just request-promise

Answer:1

Just don't add link key in object literal. add it later based on condition

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); 
}
Answer:2

I want to recursively sum an integer: to split an integer into an array and then sum the individual items until I am left with a single integer array. This is my logic: Take an integer (n) and ...

I want to recursively sum an integer: to split an integer into an array and then sum the individual items until I am left with a single integer array. This is my logic: Take an integer (n) and ...

  1. pass variable into bash script
  2. pass variable into awk
  3. pass variable into powershell script
  4. pass variable into sed
  5. pass variable into invoke-command
  6. pass variable into dockerfile
  7. pass variable into string python
  8. pass variable into settimeout
  9. pass variable into makefile
  10. pass variable into class python
  11. pass variable into promise
  12. pass variable into html
  13. pass variable into python script
  14. pass variable into sql query
  15. pass variable into lambda java
  16. pass variable into function powershell
  17. pass variable into openquery
  18. pass variable into onclick function
  19. pass variable into sql script
  20. pass variable into batch file

Below is my JS and then my HTML . I cannot figure out why my countdown clock doesn't automatically count down. You must refresh every second to see the correct amount of time left. Any ideas? BTW this ...

Below is my JS and then my HTML . I cannot figure out why my countdown clock doesn't automatically count down. You must refresh every second to see the correct amount of time left. Any ideas? BTW this ...

  1. javascript countdown clock code
  2. javascript countdown clock
  3. javascript countdown clock example
  4. javascript countdown clock explained
  5. js countdown clock
  6. styled javascript countdown clock
  7. javascript countdown flip clock
  8. html javascript countdown clock
  9. simple javascript countdown clock
  10. javascript digital countdown clock
  11. free javascript countdown clock
  12. javascript timer countdown clock
  13. javascript countdown analog clock
  14. javascript display countdown clock
  15. javascript multiple countdown clock
  16. moment js countdown clock
  17. react js countdown clock
  18. javascript countdown timer flip clock
  19. javascript countdown to date flip clock
  20. javascript countdown/count-up timer/clock/ticker for web pages

I'd like to replace all blocked ads (for users with adblock) with an image politely asking to have the site white-listed. This code wasn't written by me at all- it's taken from a site that I can't ...

I'd like to replace all blocked ads (for users with adblock) with an image politely asking to have the site white-listed. This code wasn't written by me at all- it's taken from a site that I can't ...

async function test() { const res = await fetch('https://www.easy-mock.com/mock/5c6d317e8d040716434d0a5b/reading/category/homeSmallCategory'); console.log(res) // data return res; } ...

async function test() { const res = await fetch('https://www.easy-mock.com/mock/5c6d317e8d040716434d0a5b/reading/category/homeSmallCategory'); console.log(res) // data return res; } ...