Recursively remove null values from JavaScript object

前端 未结 8 924
挽巷
挽巷 2020-12-28 16:07

I have a JSON obj, after some operations (like delete some pieces), I print it and everything looks good except that I have some null values. How do I remove th

8条回答
  •  情书的邮戳
    2020-12-28 16:29

    We can use JSON.stringify and JSON.parse together to recursively remove blank attributes from an object.

    jsObject = JSON.parse(JSON.stringify(jsObject), (key, value) => {
                   if (value == null || value == '' || value == [] || value == {})
                       return undefined;
                   return value;
               });
    

提交回复
热议问题