Open In App

How to get an array of function property names from own enumerable properties of an object in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

Enumerable Properties: All the properties of an object that can be iterated using for..in loop or Object.keys() method are known as enumerable properties.

In this article, we will see how to get an array of functions of an object that are enumerable. It can be achieved by following these 2 steps.

  • Using Object.keys() method, we can get an array of all enumerable properties (containing functions and data members) of an Object.

    Syntax:

    Object.keys(object_name)
  • Then we can filter the above obtained array using Array filter() method so that it contains only enumerable function names (eliminating all data members).

    Syntax:

    new_arr = arr_name.filter( (element)=> {.....} )

In the following implementation for creating a non-enumerable property, we have used Object.defineProperty() method.

Example:

Javascript




<script>
  
    // Person object has name, age, salary
    // and print properties 
    // Except salary, all properties are enumerable
    let Person = {
        name: "Mahesh",
        age: 25,
        print: function () {
            console.log(`${this.name} ${(this, age)}`);
        },
    };
  
    // salary- non enumerable property
    Object.defineProperty(Person, "salary", {
        value: "Rs. 50,000",
        enumerable: false,
    });
  
    // greeting- non enumerable function
    Object.defineProperty(Person, "greeting", {
        value: function () {
            console.log("Welcome to GFG");
        },
        enumerable: false,
    });
  
    // arr contains all the enumerable
    // properties of Person
    let arr = Object.keys(Person);
  
    // functionsArr contains enumerable
    // function properties of Person.
    // typeof returns an string representing
    // data type.
    // Using filter() method to filter the array
    let functionsArr = arr.filter((key) => {
        return typeof Person[key] === "function";
    });
  
    console.log(arr);
    console.log(functionsArr);
</script>



Output:

["name", "age", "print"]
["print"]

Explanation:

In the above code, we have an object named Person with 3 data members (name, age and salary) and 2 functions (‘print’ and ‘greeting’)  out of which salary data-member and greeting function are non-enumerable properties.

Property Type Enumerability
name variable enumerable
age variable enumerable
salary variable non-enumerable
print function enumerable
greeting function non-enumerable

Here ‘print‘ is the only function which is enumerable. Using Object.keys(Person) we get an array containing all enumerable properties i.e. [“name”, “age”, “print”] .

Then we used Array.filter() property to filter out all variables so that array has only function property names, i.e. [“print”].



Last Updated : 19 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads