Saturday, March 14, 2020

JavaScript tutorials - Object getOwnPropertyNames method usage examples

In this blog post, will take you an example for the following things.
Object getOwnPropertyNames method usage examples

  1. getOwnPropertyNames in object syntax and examples
  2. Difference between Object.getOwnPropertyNames() vs Object.keys() methods.
  3. Convert the Object keys into array sort order.
  4. List out all functions of an object Length of an object

getOwnPropertyNames() in an object class


getOwnPropertyNames() is a inbuilt method in Object class in javascript.

Every type in javascript extends object, Array, a map that extends Object has getOwnpropertynames method.

An object has enumerable and non-enumerable properties. Properties are attributes, and functions.
Syntax
Object.getOwnPropertyNames(Object)  

Input type - Accepts an object as an input parameter
Returns an array of all enumerable properties, not-enumerable properties, Please check Object properties guide.
Let's see an example for Enumerable keys display in object

const employee = {"name": "Franc","department":"sales"};
console.log(Object.getOwnPropertyNames(employee));  
[ 'name', 'department' ]
Let's add non enumerable property to an existin object using Object.defineProperty() method.
Object.defineProperty(employee,"address",{country:"India",enumerable:false})

getOwnPropertyNames() method prints the array of enumerable and non-enumerable properties
console.log(Object.getOwnPropertyNames(employee));  
[ 'name', 'department', 'address' ]

Difference between Object.getOwnPropertyNames() vs Object.keys() methods.

Object.keys() returns the array of enumerable properties keys only Object.getOwnPropertyNames() returns the array of enumerable and non-enumerable properties keys.

const employee = {"name": "Franc","department":"sales"};
Object.defineProperty(employee,"address",{country:"India",enumerable:false})
console.log(Object.keys(employee));

console.log(Object.getOwnPropertyNames(employee));  
Output is
[ 'name', 'department' ]
[ 'name', 'department', 'address' ]

Convert the Object keys into array sort order

getOwnPropertyNames() returns array of keys.
pass returned array with sort or reverse method for sorting and returns array sorted as a seen example.

console.log(Object.getOwnPropertyNames(employee));  
console.log(Object.getOwnPropertyNames(employee).sort());  
output is


[ 'name', 'department', 'address' ]
[ 'address', 'department', 'name' ]
Let's see an example object to array with values by sorting key order
Object.getOwnPropertyNames(employee).sort().forEach(function(element){
         console.log((employee[element]));
      })

List out all functions of an object 

An array is an object, This example prints out all function or methods of an array using getOwnPropertyNames() method
console.log(Object.getOwnPropertyNames(Array));  
//[ 'length', 'name', 'prototype', 'isArray', 'from', 'of' ]

console.log(Object.getOwnPropertyNames(Array).filter(function (key) {
    return typeof Math[key] === 'function';
}));
//[ 'isArray', 'from', 'of' ]

Length of an properties of an object

 getOwnPropertyNames() returns array of properties of an object, calling length on an object gives length of an array.
console.log(Object.getOwnPropertyNames(employee).length);  //3

Related article


EmoticonEmoticon