Science Knowings: JavaScript Course For Social Media

Object Methods (Object.keys, Object.values, Object.entries)

Previous: Array Methods

We've covered Array Methods like map, filter, and reduce that empower us to transform and manipulate arrays. Now, let's dive into Object Methods that provide similar functionality for objects.

Object Methods

Object Methods: Unlocking Object Exploration

Object Methods are built-in functions that allow us to interact with objects in a more structured and efficient manner. They simplify tasks like extracting keys, values, and key-value pairs from objects.

Importance of Object Methods

Object Methods offer numerous advantages:

  • Simplify object manipulation
  • Enhance code readability and maintainability
  • Provide a consistent and standardized way to work with objects


Introducing Object.keys()

The Object.keys() method extracts all the property names (keys) of an object into an array.

Object.keys() Syntax




const keys = Object.keys({ name: 'Alice', age: 30 });

Object.keys() Example


const person = { name: 'John', age: 35, occupation: 'Doctor' };const personKeys = Object.keys(person);console.log(personKeys); // Output: ['name', 'age', 'occupation']


Unveiling Object.values()

Object.values() returns an array containing the values of all the properties in an object.

Object.values() Syntax




const values = Object.values({ name: 'Alice', age: 30 });

Object.values() Example


const person = { name: 'John', age: 35, occupation: 'Doctor' };const personValues = Object.values(person);console.log(personValues); // Output: ['John', 35, 'Doctor']


Introducing Object.entries()

Object.entries() provides a powerful way to iterate over an object's key-value pairs.

Object.entries() Syntax




const entries = Object.entries({ name: 'Alice', age: 30 });

Object.entries() Example


const person = { name: 'John', age: 35, occupation: 'Doctor' };const personEntries = Object.entries(person);console.log(personEntries); // Output: [['name', 'John'], ['age', 35], ['occupation', 'Doctor']]

Practical Applications

Unlocking the Power of Object Methods

Object Methods have wide-ranging applications in object manipulation:

  • Creating arrays of property names
  • Extracting values for further processing
  • Iterating over key-value pairs
  • Sorting and filtering objects

Case Study: Sorting Keys

Case Study:

Problem: Sort the properties of an object alphabetically.


const sortedKeys = Object.keys(object).sort();

Hands-on Exercise

Interactive Exercise

Extract the names from an object containing student data using Object.keys().

Next: Working with Dates

Our next stop is Working with Dates. Understanding dates is crucial for handling timestamps, scheduling tasks, and manipulating time-based data.

Follow us for more exciting content!