DEV Community

phatsss
phatsss

Posted on

Replace empty string to undefined value in Object (JS)

just create function like:

 const replaceEmpty = (obj) => {
    let newObj = {};
    Object.keys(obj).forEach((key) => {
      if (obj[key] === Object(obj[key])) newObj[key] = removeEmpty(obj[key]);
      else if (obj[key] !== undefined) newObj[key] = obj[key];
      else if (obj[key] === undefined) newObj[key] = '';
    });
    return newObj;
  };
Enter fullscreen mode Exit fullscreen mode

that's all, hope this func can help U 😵‍💫

Top comments (0)