- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove number properties from an object JavaScript
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 } }
- Related Articles
- Extract properties from an object in JavaScript
- Returning the highest number from object properties value – JavaScript
- JavaScript Object Properties
- How to remove all blank objects from an Object in JavaScript?
- Shortest syntax to assign properties from function call to an existing object in JavaScript
- How to remove certain number elements from an array in JavaScript
- What are the properties of an array object in JavaScript?
- How to remove a property from a JavaScript object?
- How do we remove a property from a JavaScript object? - JavaScript
- How to remove an object using filter() in JavaScript?
- Filter the properties of an object based on an array and get the filtered object JavaScript
- How to add properties and methods to an object in JavaScript?
- How to create an object and access its properties in JavaScript?
- How do I remove a property from a JavaScript object?
- How to duplicate Javascript object properties in another object?

Advertisements