Horje
How to Find the Length of JavaScript Dictionary ?

In JavaScript, you may often find a dictionary referred to as an object. Therefore, unlike arrays which have a length property, objects (dictionaries) do not have a built-in length property. However, the “length” of a given dictionary in JavaScript may be determined through counting the pairs of key-value it contains.

Below are the approaches used to find the length of JavaScript dictionary

Approach 1: Using Object.keys()

JavaScript uses the Object.keys() method to extract an object’s enumerable property names and promptly returns them as an array. This operation involves the object’s own properties, thereby excluding any inherited from its prototype chain. Consequently, one can utilize this resultant array for determining either the length or count of these specific properties.

Example: Here, the function Object.keys(dictionary) generates an array that holds the keys of a dictionary object; subsequently, we apply the .length property to obtain a key count which represents our dictionary’s length.

Javascript

const dictionary = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
};
 
const length = Object.keys(dictionary).length;
console.log("Length of the dictionary:", length);

Output

Length of the dictionary: 3

Aproach 2: Using Object.getOwnPropertyNames()

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.

Example: Object.getOwnPropertyNames(dictionary) in this code retrieves an array of all property names, inclusive of non-enumerable ones; subsequently, the .length property calculates the count of said properties within the array. This count directly represents and signifies: it is indeed the length–or size–of our dictionary. We then proceed to log this result onto the console.

Javascript

const dictionary = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
};
 
const length =
    Object.getOwnPropertyNames(dictionary).length;
console.log("Length of the dictionary:", length);

Output

Length of the dictionary: 3

Approach 3: Using hasOwnProperty()

The hasOwnProperty() method is used to check if an object has a specific property as its own property.

Example: Here, objectLength is incremented for each property encountered during the iteration. After the loop, the total length is then outputted to the console.

Javascript

// Creating an object
let myObject = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
};
 
// Initializing a counter variable
let objectLength = 0;
 
// Iterating through the object using hasOwnProperty()
for (let key in myObject) {
  if (myObject.hasOwnProperty(key)) {
    objectLength++;
  }
}
 
// Outputting the length of the object
console.log("The length of the object is: " + objectLength);

Output

The length of the object is: 3



Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
Creating Subtasks and Defining Acceptance Criteria in Jira for User Stories Creating Subtasks and Defining Acceptance Criteria in Jira for User Stories
React Bootstrap Close Button Component React Bootstrap Close Button Component
What is Critical Path in Project Management? What is Critical Path in Project Management?
Operating System Error Handling Operating System Error Handling
Longest Palindromic Subsequence in JavaScript Longest Palindromic Subsequence in JavaScript

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14