In this chapter, we will explore the Object.entries()
method in TypeScript. This method returns an array of a given object's own enumerable string-keyed property [key, value]
pairs. Understanding how to use Object.entries()
is useful for iterating over an object's properties and values.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The Object.entries()
method returns an array of a given object's own enumerable string-keyed property [key, value]
pairs. This is useful for iterating over an object's properties and values.
2. Syntax
Object.entries(obj);
Parameters
obj
: The object whose own enumerable string-keyed property[key, value]
pairs are to be returned.
Return Value
The method returns an array of the given object's own enumerable string-keyed property [key, value]
pairs.
3. Examples
Let's look at some examples to understand how Object.entries()
works in TypeScript.
Example 1: Basic Usage
This example shows how to use Object.entries()
to get an array of [key, value]
pairs from an object.
let person = {
name: "Ravi",
age: 25,
city: "Mumbai"
};
let entries = Object.entries(person);
console.log(entries);
// Output:
// [ ['name', 'Ravi'], ['age', 25], ['city', 'Mumbai'] ]
Example 2: Iterating Over Entries
This example shows how to iterate over the [key, value]
pairs returned by Object.entries()
.
let person = {
name: "Ravi",
age: 25,
city: "Mumbai"
};
for (let [key, value] of Object.entries(person)) {
console.log(`${key}: ${value}`);
}
// Output:
// name: Ravi
// age: 25
// city: Mumbai
Example 3: Converting Object to Map
This example shows how to convert an object to a Map using Object.entries()
.
let person = {
name: "Ravi",
age: 25,
city: "Mumbai"
};
let personMap = new Map(Object.entries(person));
console.log(personMap);
// Output: Map { 'name' => 'Ravi', 'age' => 25, 'city' => 'Mumbai' }
Example 4: Filtering Entries
This example shows how to filter the entries of an object using Object.entries()
.
let person = {
name: "Ravi",
age: 25,
city: "Mumbai"
};
let filteredEntries = Object.entries(person).filter(([key, value]) => typeof value === 'number');
console.log(filteredEntries);
// Output: [ ['age', 25] ]
Example 5: Converting Entries to Object
This example shows how to convert an array of entries back to an object using Object.fromEntries()
.
let person = {
name: "Ravi",
age: 25,
city: "Mumbai"
};
let entries = Object.entries(person);
let newPerson = Object.fromEntries(entries);
console.log(newPerson);
// Output: { name: 'Ravi', age: 25, city: 'Mumbai' }
Example 6: Handling Non-Enumerable Properties
This example shows how Object.entries()
handles non-enumerable properties.
let person = {
name: "Ravi",
age: 25
};
Object.defineProperty(person, 'city', {
value: "Mumbai",
enumerable: false
});
let entries = Object.entries(person);
console.log(entries);
// Output: [ ['name', 'Ravi'], ['age', 25] ]
4. Conclusion
In this chapter, we explored the Object.entries()
method in TypeScript, which is used to return an array of a given object's own enumerable string-keyed property [key, value]
pairs. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use Object.entries()
is essential for iterating over an object's properties and values in TypeScript applications.
Comments
Post a Comment
Leave Comment