TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Javascript Object getOwnPropertyNames() Method

Javascript Object getOwnPropertyNames() Method with example on javascript, object, getOwnPropertySymbols(), getPrototypeOf(), defineProperties(), defineProperty(), entries(), freeze(), javascript tutorial, javascript array etc.

<< Back to JAVASCRIPT

JavaScript Object.getOwnPropertyNames() Method

The Object.getOwnPropertyNames() method returns an array of all properties (except those non-enumerable properties which use symbol) found directly upon a given object.

Syntax:

Object.getOwnPropertyNames(obj)

Parameter:

obj: It is the object whose enumerable and non-enumerable own properties are to be returned.

Return value:

This method returns an array of string that correspond to the properties found directly upon the object.

Browser Support:

Chrome 5
Edge Yes
Firefox 4
Opera 12

Example 1

const object1 = {
  a: 0,
  b: 1,
  c: 2,
};
console.log(Object.getOwnPropertyNames(object1));

Output:

["a", "b", "c"]

Example 2

 var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.getOwnPropertyNames(obj).sort()); // logs '0,1,2'

// Logging property names and values using Array.forEach

Object.getOwnPropertyNames(obj).forEach(function(val, idx, array) {
  console.log(val + ' -> ' + obj[val]);

});

Output:

["0", "1", "2"]
 "0 -> a"
 "1 -> b"
 "2 -> c"    

Example 3

function Pasta(grain, size, shape) {
    this.grain = grain; 
    this.size = size; 
    this.shape = shape; 
}
var spaghetti = new Pasta("wheat", 2, "circle");
var names = Object.getOwnPropertyNames(spaghetti).filter(CheckKey);
document.write(names); 
 // Check whether the first character of a string is 's'. 
function CheckKey(value) {
    var firstChar = value.substr(0, 1); 
    if (firstChar.toLowerCase() == 's')
        return true; 
    else
         return false; }

Output:

size,shape

Next TopicJavaScript Objects




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf