We are given an object that contains some random properties, including some numbers, boolean, strings and the object itself.
We are required to write a function that takes in the object as first argument and a string as second argument, possible value for second argument is a name of any data type in JavaScript like number, string, object, boolean, symbol etc.
Our task is to delete every property of type specified by the second argument. If the second argument is not provided, take ‘number’ as default.
The full code for doing so will be −
const obj = { name: 'Lokesh Rahul', age: 29, mother: 'Avantika Rahul', father: 'Trilok Rahul', matches: 123, average: 45.23, isFit: true, runs: { odi: 5674, test: 3456 } }; const shedData = (obj, type = 'number') => { for(const key in obj){ if(typeof obj[key] === type){ delete obj[key]; }; }; }; shedData(obj, 'string'); console.log(obj);
Output
The output in the console will be −
{ age: 29, matches: 123, average: 45.23, isFit: true, runs: { odi: 5674, test: 3456 } }