Home > Software engineering >  Filter Null/Undefined Values in React and ES6
Filter Null/Undefined Values in React and ES6

Time:12-31

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

CodePudding user response:

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