DEV Community

Aman Kumar
Aman Kumar

Posted on

Mastering JavaScript Objects: From Singletons to Object Arrays 🎯Part 2

JavaScript objects are essential to structuring data in your programs. Today, we’ll cover singleton objects, object nesting, combining objects, and working with arrays of objects. Plus, we'll dive into some helpful object methods! Let’s break it down step by step with a fun and interactive approach! 🚀


1. Singleton vs. Non-Singleton Objects 🧑‍💻

When working with objects, you may encounter singleton and non-singleton objects.

  • Singleton Object: Created using a constructor like new Object(). This means it’s unique in a sense.
  const tinderUser = new Object(); // Singleton Object
Enter fullscreen mode Exit fullscreen mode
  • Non-Singleton Object: Created using object literals {}. You can create as many as you like, and each will be a different instance.
  const tinderUserTwo = {}; // Non-singleton Object
Enter fullscreen mode Exit fullscreen mode

You can easily add properties to objects:

tinderUserTwo.id = "123abc";
tinderUserTwo.name = "Sammy";
tinderUserTwo.isLoggedIn = false;
Enter fullscreen mode Exit fullscreen mode

📌 Output: { id: '123abc', name: 'Sammy', isLoggedIn: false }


2. Objects Inside Objects 🌐

JavaScript allows you to nest objects within other objects, which is useful for organizing complex data. For example:

const regularUser = {
    email: "someuser@gmail.com",
    fullName: {
        usersName: {
            firstName: "Ayush",
            lastName: "Yadav"
        }
    }
};
Enter fullscreen mode Exit fullscreen mode

To access nested properties, you can chain property names like this:

console.log(regularUser.fullName.usersName.lastName); // Output: Yadav
Enter fullscreen mode Exit fullscreen mode

This makes data retrieval smooth and structured. 🗂️


3. Combining Objects 🤝

Want to merge multiple objects together? There are two common ways to do that:

  • Using Object.assign():
  const obj1 = { 1: "a", 2: "b" };
  const obj2 = { 3: "a", 4: "b" };
  const obj3 = Object.assign({}, obj1, obj2); // Combine obj1 and obj2 into obj3
Enter fullscreen mode Exit fullscreen mode
  • Using the spread operator (✨ most popular method):
  const obj3 = { ...obj1, ...obj2 }; // Merge obj1 and obj2
Enter fullscreen mode Exit fullscreen mode

📌 Output: { '1': 'a', '2': 'b', '3': 'a', '4': 'b' }


4. Array of Objects 📋

In real-world applications, you often have multiple objects stored in an array. For example:

const users = [
    { id: 1, email: "h@gmail.com" },
    { id: 2, email: "i@gmail.com" },
    { id: 3, email: "j@gmail.com" },
    { id: 4, email: "k@gmail.com" }
];
Enter fullscreen mode Exit fullscreen mode

You can access individual objects by their index:

console.log(users[2].email); // Output: j@gmail.com
Enter fullscreen mode Exit fullscreen mode

This is incredibly useful for handling multiple data points. 🌟


5. Essential Object Methods 🛠️

JavaScript provides several helpful methods to work with objects efficiently:

  1. Object.keys(): Returns an array of the object’s keys.
   console.log(Object.keys(tinderUserTwo)); // Output: [ 'id', 'name', 'isLoggedIn' ]
Enter fullscreen mode Exit fullscreen mode
  1. Object.values(): Returns an array of the object’s values.
   console.log(Object.values(tinderUserTwo)); // Output: [ '123abc', 'Sammy', false ]
Enter fullscreen mode Exit fullscreen mode
  1. Object.entries(): Returns an array of key-value pairs.
   console.log(Object.entries(tinderUserTwo));
   // Output: [ ['id', '123abc'], ['name', 'Sammy'], ['isLoggedIn', false] ]
Enter fullscreen mode Exit fullscreen mode
  1. hasOwnProperty(): Checks if a property exists in the object.
   console.log(tinderUserTwo.hasOwnProperty('isLoggedIn')); // Output: true
Enter fullscreen mode Exit fullscreen mode

Recap 🎯

In this post, we explored:

  • Singleton vs. Non-Singleton objects 💻
  • Nesting objects for structured data 🌐
  • Combining objects using Object.assign and the spread operator 🤝
  • Working with arrays of objects 📋
  • Essential methods like Object.keys(), Object.values(), and hasOwnProperty() 🛠️

These concepts are fundamental to mastering JavaScript objects. Keep practicing and experimenting with these techniques to enhance your skills! 🎉

Top comments (1)

Collapse
 
ndotie profile image
ndotie

thanks for talking about Object.entries(): Returns an array of key-value pairs. This will be very helpful