Remove number properties from an object JavaScript

In JavaScript, we often need to filter object properties based on their data types. This article demonstrates how to remove properties of a specific type from an object using a reusable function.

Problem Statement

Given an object with mixed property types (numbers, strings, booleans, objects), we need to create a function that removes all properties of a specified data type. If no type is specified, it should default to removing number properties.

Solution

We'll create a function called shedData that iterates through object properties and deletes those matching the specified type:

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];
        }
    }
};

// Remove string properties
shedData(obj, 'string');
console.log(obj);
{
  age: 29,
  matches: 123,
  average: 45.23,
  isFit: true,
  runs: { odi: 5674, test: 3456 }
}

Example: Removing Number Properties (Default Behavior)

const playerData = {
    name: 'John Doe',
    age: 25,
    score: 89.5,
    team: 'Eagles',
    isActive: true,
    stats: {
        goals: 15,
        assists: 8
    }
};

const shedData = (obj, type = 'number') => {
    for(const key in obj){
        if(typeof obj[key] === type){
            delete obj[key];
        }
    }
};

// Remove number properties (default behavior)
shedData(playerData);
console.log(playerData);
{
  name: 'John Doe',
  team: 'Eagles',
  isActive: true,
  stats: { goals: 15, assists: 8 }
}

Example: Removing Boolean Properties

const settings = {
    theme: 'dark',
    fontSize: 14,
    autoSave: true,
    notifications: false,
    username: 'user123'
};

const shedData = (obj, type = 'number') => {
    for(const key in obj){
        if(typeof obj[key] === type){
            delete obj[key];
        }
    }
};

// Remove boolean properties
shedData(settings, 'boolean');
console.log(settings);
{
  theme: 'dark',
  fontSize: 14,
  username: 'user123'
}

How It Works

The function uses a for...in loop to iterate through all enumerable properties of the object. For each property, it checks if typeof obj[key] matches the specified type and uses the delete operator to remove matching properties.

Key Points

  • The delete operator removes properties from objects permanently
  • Default parameter syntax (type = 'number') provides fallback behavior
  • The function modifies the original object (mutation)
  • Nested objects are not traversed - only top-level properties are checked

Conclusion

This approach provides a flexible way to filter object properties by data type. The function can be easily customized to remove any JavaScript data type by passing the appropriate type string.

Updated on: 2026-03-15T23:18:59+05:30

310 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements