🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Table of Contents
- Using
Object.values() - Using
Object.keys()with Mapping - Using
for...inLoop - Using
Object.entries() - Conclusion
1. Using Object.values()
The Object.values() method is the easiest and most efficient way to get all values from an object. It returns an array of the object’s own enumerable property values.
Example:
const employee = {
firstName: "Ramesh",
lastName: "Fadatare",
email: "ramesh.fadatare@gmail.com"
};
const values = Object.values(employee);
console.log(values);
Output:
["Ramesh", "Fadatare", "ramesh.fadatare@gmail.com"]
Explanation:
Object.values(employee) returns an array of the object’s values. This method only includes the object's own properties, excluding any inherited properties.
2. Using Object.keys() with Mapping
If you want to get the values of an object without using Object.values(), you can first get the keys using Object.keys(), and then map over them to extract the values.
Example:
const employee = {
firstName: "Ramesh",
lastName: "Fadatare",
email: "ramesh.fadatare@gmail.com"
};
const values = Object.keys(employee).map(key => employee[key]);
console.log(values);
Output:
["Ramesh", "Fadatare", "ramesh.fadatare@gmail.com"]
Explanation:
This method uses Object.keys() to get an array of keys and then maps each key to its corresponding value in the employee object.
3. Using for...in Loop
The for...in loop iterates over all enumerable properties of an object, including inherited ones. To ensure you only get the object's own properties, you should check with hasOwnProperty().
Example:
const employee = {
firstName: "Ramesh",
lastName: "Fadatare",
email: "ramesh.fadatare@gmail.com"
};
const values = [];
for (let key in employee) {
if (employee.hasOwnProperty(key)) {
values.push(employee[key]);
}
}
console.log(values);
Output:
["Ramesh", "Fadatare", "ramesh.fadatare@gmail.com"]
Explanation:
This method uses a for...in loop to iterate over the object and pushes the values to the values array. The hasOwnProperty() check ensures only the object's own properties are considered.
4. Using Object.entries()
The Object.entries() method returns an array of the object's own enumerable properties as key-value pairs. You can use this to get all values by mapping over the entries.
Example:
const employee = {
firstName: "Ramesh",
lastName: "Fadatare",
email: "ramesh.fadatare@gmail.com"
};
const values = Object.entries(employee).map(([key, value]) => value);
console.log(values);
Output:
["Ramesh", "Fadatare", "ramesh.fadatare@gmail.com"]
Explanation:
Object.entries(employee) returns an array of key-value pairs. By mapping over this array, you can extract just the values.
Conclusion
There are multiple ways to get all values from an object in JavaScript. The most direct and efficient method is using Object.values(), but you can also use other approaches like mapping over Object.keys() or using a for...in loop if needed.
Summary of Methods:
Object.values(): Returns an array of the object's own enumerable values.Object.keys()with Mapping: Retrieves the keys and maps them to their corresponding values.for...inloop: Iterates over the object's properties and collects the values.Object.entries(): Extracts values from key-value pairs.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment