Object and its common methods in javascript

Object and its common methods in javascript

Object:

  • Object has key-value pairs.

  • In JavaScript objects, the keys are always stored and treated as strings, regardless of how they are specified. This means that even if you define a key as a number or a symbol, it will be converted to a string internally.

  • The data-type of value could be of any type like string, number, object, function, array or boolean.

Various Methods To Declare An Object

  1. Object Literal Notation :

     const person = {
         firstName: 'Vivek',
         lastName: 'Bindra'
     };
    
     console.log(person);
     // Output: {firstName: 'Vivek', lastName: 'Bindra'}
    
  2. new Object() syntax :

     const car = new Object();
     car.make = 'Toyota';
     car.model = 'Camry';
     car.year = 2022;
    
     console.log(car);
     // Output: {make: 'Toyota', model: 'Camry', year: 2022}
    
  3. Object.create() method :

     const animal = Object.create(null);
     animal.type = 'Dog';
     animal.sound = 'Bark';
    
  4. Using constructor function :

     function Person(firstName, lastName, age){
         this.firstName = firstName;
         this.lastName = lastName;
         this.age = age;
     }
     const person1 = new Person('Radhika', 'Pawar', 27)
    

How To Access Object Values

  1. Dot notation :

     const user = {
         firstName: "Harshal",
         lastName: "Dongare"
     } 
     console.log(user.firstName);     // Output: "Harshal"
    
  2. Bracket Notation :

     const user = {
         firstName: "Harshal",
         lastName: "Dongare"
         "full name": "Harshal Dongare"
     } 
     console.log(user["lastName"]);     // Output: "Dongare"
     console.log(user["full name"]);     // Output: "Harshal Dongare"
    

How To use Key as a Symbol In Object

const mySym = Symbol("key1");

const obj1 = {
    firstName: "Harshal",
    lastName: "Dongare",
    [mySym] : "myKey1"
}
console.log(typeof obj1[mysym]); // Output: "myKey1"

Method To Apply On Object

  1. Object.freeze(objName) :

    A method that is used to freeze an object. When an object is frozen, it's properties cannot be added, removed, or modified.

     // Creating a sample object
     const person = {
       name: 'John',
       age: 30
     };
    
     // Freeze the object
     Object.freeze(person);
    
     // Try to modify the object
     person.age = 40; // This change will not take effect
    
     // Try to add a new property
     person.gender = 'Male'; // This addition will not take effect
    
     // Try to delete a property
     delete person.name; // This deletion will not take effect
    
     // The object remains unchanged
     console.log(person); // Output: { name: 'John', age: 30 }
    
  2. Object.keys(objName) :

    It is a built-in javascript method that returns an array of all keys present inside an object.

     const person = {
       firstName: 'John',
       lastName: 'Doe',
       age: 30,
     };
    
     const keys = Object.keys(person);
     console.log(keys); // Output: ['firstName', 'lastName', 'age']
    
  3. Object.values(objName) :

    A method that returns an array of all given values of keys present inside an object.

     const person = {
       firstName: 'John',
       lastName: 'Doe',
       age: 30,
     };
    
     const values = Object.values(person);
     console.log(values); // Output: ['John', 'Doe', 30]
    
  4. Object.entries() :

    A method returns an array of a given object's key-value pairs, in the form of [key, value] arrays.

     const person = {
       firstName: 'John',
       lastName: 'Doe',
       age: 30,
     };
    
     const entries = Object.entries(person);
     console.log(entries);
     // Output:
     // [
     //   ['firstName', 'John'],
     //   ['lastName', 'Doe'],
     //   ['age', 30]
     // ]
    
  5. hasOwnProperty(propertyName) :

    A method to check whether the given property name present inside an object or not. It returns boolean value based upon the results.

     const person = {
       firstName: 'John',
       lastName: 'Doe',
       age: 30,
     };
    
     console.log(person.hasOwnProperty('firstName')); // Output: true
     console.log(person.hasOwnProperty('middleName')); // Output: false
    

How To Merge Two Objects

  1. Object.assign() method :

    This method copies all enumerable own properties from one or more source objects to a target object and returns the target object.

     const target = { a: 1, b: 2 };
     const source = { b: 3, c: 4 };
     const merged = Object.assign({}, target, source);
     console.log(merged); // Output: { a: 1, b: 3, c: 4 }
    
  2. spread syntax :

    The spread syntax allows for easy merging of objects by spreading the properties of one or more objects into a new object.

     const target = { a: 1, b: 2 };
     const source = { b: 3, c: 4 };
     const merged = { ...target, ...source };
     console.log(merged); // Output: { a: 1, b: 3, c: 4 }
    

Destructuring Of An Array

Object destructuring is a feature in JavaScript that allows you to extract multiple properties from an object and assign them to variables in a more concise and readable way.

Syntax:

const { property1, property2 } = object;
  1. Basic Object destructuring

     const person = {
       firstName: 'John',
       lastName: 'Doe',
       age: 30,
     };
    
     const { firstName, lastName } = person;
    
     console.log(firstName); // Output: 'John'
     console.log(lastName); // Output: 'Doe'
    
  2. Renaming Variables

     const person = {
       firstName: 'John',
       lastName: 'Doe',
       age: 30,
     };
    
     const { firstName: fName, lastName: lName } = person;
    
     console.log(fName); // Output: 'John'
     console.log(lName); // Output: 'Doe'
    
  3. Nested Destructuring

     const person = {
       name: {
         first: 'John',
         last: 'Doe',
       },
       age: 30,
     };
    
     const { name: { first, last } } = person;
    
     console.log(first); // Output: 'John'
     console.log(last); // Output: 'Doe'