Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter Null/Undefined Values in React and ES6

I’m filtering null and undefined values so I don’t need to pass variables that have null/undefined values. My problem is if my value is 0 I still want to pass it. Only null/undefined values should be filtered

const filterNonNull = (obj) => {
    return Object.fromEntries(Object.entries(obj).filter(([k, v]) => v));
  };
  
  export const printService = ({ person_id, print_count, group_id }) => {
    return getAxiosService().get(
      `print/${person_id}?${qs.stringify(filterNonNull({ print_count, group_id }))}`
    );
  };

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Update your condition like .filter(([k, v]) => !(v === null || v === undefined))).

const filterNonNull = (obj) => {
    return Object.fromEntries(Object.entries(obj).filter(([k, v]) => !(v === null || v === undefined)));
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading