79606725

Date: 2025-05-05 10:13:12
Score: 2.5
Natty:
Report link

@Aplet123's answer works, but it filters out all falsy values. So, I'd suggest a small change:

function getFilteredObject(obj) {
    // we're filtering out all null, undefined and empty strings but not 0 and false
    return Object.fromEntries(Object.entries(obj).filter(([k, v]) => ![null, undefined, ""].includes(v)));
}

// you could also use `skipNull: true` as mentioned by others to only skip null values
const url = `/user?${qs.stringify(getFilteredObject({ name, age }))}`;
Reasons:
  • Has code block (-0.5):
  • User mentioned (1): @Aplet123's
  • Looks like a comment (1):
  • Low reputation (1):
Posted by: Sync271