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