TypeScript Object keys()

In this chapter, we will explore the Object.keys() method in TypeScript. This method returns an array of a given object's own enumerable property names (keys). Understanding how to use Object.keys() is useful for iterating over an object's properties.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The Object.keys() method returns an array of a given object's own enumerable property names (keys). This is useful for iterating over an object's properties.

2. Syntax

Object.keys(obj);

Parameters

  • obj: The object whose own enumerable property names are to be returned.

Return Value

The method returns an array of the given object's own enumerable property names.

3. Examples

Let's look at some examples to understand how Object.keys() works in TypeScript.

Example 1: Basic Usage

This example shows how to use Object.keys() to get an array of keys from an object.

let person = {
  name: "Ravi",
  age: 25,
  city: "Mumbai"
};

let keys = Object.keys(person);
console.log(keys);
// Output: [ 'name', 'age', 'city' ]

Example 2: Iterating Over Keys

This example shows how to iterate over the keys returned by Object.keys().

let person = {
  name: "Ravi",
  age: 25,
  city: "Mumbai"
};

for (let key of Object.keys(person)) {
  console.log(key);
}
// Output:
// name
// age
// city

Example 3: Using Object.keys() with Arrays

This example shows how Object.keys() can be used with arrays.

let fruits = ["apple", "banana", "mango"];
let keys = Object.keys(fruits);
console.log(keys);
// Output: [ '0', '1', '2' ]

Example 4: Converting Keys to Uppercase

This example shows how to convert the keys of an object to uppercase using Object.keys().

let person = {
  name: "Ravi",
  age: 25,
  city: "Mumbai"
};

let uppercaseKeys = Object.keys(person).map(key => key.toUpperCase());
console.log(uppercaseKeys);
// Output: [ 'NAME', 'AGE', 'CITY' ]

Example 5: Handling Non-Enumerable Properties

This example shows how Object.keys() handles non-enumerable properties.

let person = {
  name: "Ravi",
  age: 25
};

Object.defineProperty(person, 'city', {
  value: "Mumbai",
  enumerable: false
});

let keys = Object.keys(person);
console.log(keys);
// Output: [ 'name', 'age' ]

Example 6: Checking if an Object is Empty

This example shows how to check if an object is empty using Object.keys().

let emptyObj = {};
let isEmpty = Object.keys(emptyObj).length === 0;
console.log(isEmpty);
// Output: true

Example 7: Using Object.keys() with Nested Objects

This example shows how to use Object.keys() with nested objects.

let person = {
  name: "Ravi",
  address: {
    city: "Mumbai",
    zip: "400001"
  }
};

let keys = Object.keys(person);
console.log(keys);
// Output: [ 'name', 'address' ]

let addressKeys = Object.keys(person.address);
console.log(addressKeys);
// Output: [ 'city', 'zip' ]

4. Conclusion

In this chapter, we explored the Object.keys() method in TypeScript, which is used to return an array of a given object's own enumerable property names (keys). We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use Object.keys() is essential for iterating over an object's properties in TypeScript applications.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare