How to useObject.getOwnPropertySymbols() method in Javascript

In JavaScript, the Object.getOwnPropertySymbols() method returns an array of all symbol properties found directly upon a given object.

Syntax:

Object.getOwnPropertySymbols(obj);

Example:

JavaScript
let symbol1 = Symbol('a');
let symbol2 = Symbol.for('b');

let obj = {
  [symbol1]: 'hello',
  [symbol2]: 'world'
};

let symbols = Object.getOwnPropertySymbols(obj);
console.log(symbols); // Output: [Symbol(a), Symbol(b)]

Output
[ Symbol(a), Symbol(b) ]




How to get a list of associative array keys in JavaScript ?

Associative Array: Associative arrays are used to store key-value pairs. For example, to store the marks of the different subjects of a student in an array, a numerically indexed array would not be the best choice. Instead, we could use the respective subjects’ names as the keys in our associative array, and the value would be their respective marks gained. In an associative array, the key-value pairs are associated with a symbol. 

These are the following methods to get a list of Associative Array Keys:

Table of Content

  • Method 1: Using JavaScript foreach loop
  • Method 2: Using Object.keys() method
  • Method 3: Using Object.getOwnPropertyNames() method
  • Method 4: Using Object.entries() method
  • Method 5: Using Reflect.ownKeys() method
  • Method 6: Using Object.getOwnPropertySymbols() method

Similar Reads

Method 1: Using JavaScript foreach loop

In this method, traverse the entire associative array using a foreach loop and display the key elements of the array....

Method 2: Using Object.keys() method

The Object.keys() is an inbuilt function in javascript that can be used to get all the keys of the array....

Method 3: Using Object.getOwnPropertyNames() method

The Object.getOwnPropertyNames() method in JavaScript is a standard built-in object which returns all properties that are present in a given object except for those symbol-based non-enumerable properties....

Method 4: Using Object.entries() method

JavaScript Object.entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. The ordering of the properties is the same as that given by looping over the property values of the object manually....

Method 5: Using Reflect.ownKeys() method

JaScript Reflect.ownKeys() method in Javascript is used to return an array of the target object’s own property keys and it ignores the inherited properties....

Method 6: Using Object.getOwnPropertySymbols() method

In JavaScript, the Object.getOwnPropertySymbols() method returns an array of all symbol properties found directly upon a given object....