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:
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