How to remove all null and undefined values from an object in TypeScript

1 Answer

0 votes
const obj = {
  w: null,
  name: 'Roy',
  x: undefined,
  age: 60,
  y: undefined,
  workin: 'TypeScript',
};
 
Object.keys(obj).forEach(key => {
  	if (obj[key] === null || obj[key] === undefined) {
    		delete obj[key];
  }
});
 
console.log(obj);
 
   
   
   
   
/*
run:
   
{
  "name": "Roy",
  "age": 60,
  "workin": "TypeScript"
} 
   
*/

 



answered Jul 1, 2022 by avibootz
edited Jul 1, 2022 by avibootz
...