SelfTechy

A portal for self learning

Monthly Archives: February 2023

Working with Arrays and Objects in Javascript


Arrays

Arrays in JavaScript are a data structure that allows you to store multiple values in a single variable. The values can be of any type, including numbers, strings, or even other arrays.

To define an array, we can either use brackets or object notation.

ArrayExamples.js

let studentsArr = ["Manisha", "Giridhar", "Shivakumar", "Seetaram"];
let studentIds = new Array(121,122,123,124,125,126);

console.log("----------------------------------------");
console.log("Students IDs");
console.log("----------------------------------------");

for(let i=0;i<studentIds.length;i++){
    console.log(studentIds[i])
}

console.log("----------------------------------------");
console.log("Students Array");
console.log("----------------------------------------");
for(let key in studentsArr){
    console.log(studentsArr[key]);
}

There are several built-in methods for manipulating arrays, such as push(), pop(), shift(), and unshift(), that allow you to add or remove items from the array.

Using the above methods we can use the arrays as Stack, & Queue.

Below are some examples:

/**
 * The stack is a linear data structure  
 * that is used to store the collection of objects.
 * It is based on Last-In-First-Out (LIFO).
 */

//Let us take the example of Received Phone calls

let receivedCallsStack = [];

receivedCallsStack.push("First Call");
receivedCallsStack.push("Second call");
receivedCallsStack.push("Third call");
receivedCallsStack.push("Fourth call");

for(let key in receivedCallsStack){
    console.log(receivedCallsStack);
}

console.log("Most recent call is from: "+receivedCallsStack.pop());
console.log("Second most call is from: "+receivedCallsStack.pop());
/**
 * A queue is a linear data structure that stores the elements sequentially.
 * It uses the FIFO approach (First In First Out) for accessing elements.
 */

// Let us take the example of Cashier line in a store

class CashierLine {
    constructor() {
        this.customers = [];
    }
    
    // Customer stands in the line
    standInQue(customer) {
        return this.customers.push(customer);
    }
    
    // Customer moves out from the Line
    moveOut() {
        if(this.customers.length > 0) {
            return this.customers.shift();
        }
    }
    
    // view the last person in the queue
    peek() {
        return this.customers[this.customers.length - 1];
    }
    
    // check if any customer is present in the line
    isEmpty(){
       return this.customers.length == 0;
    }
   
    // the size of the queue
    size(){
        return this.customers.length;
    }
 
    // empty the queue
    clear(){
        this.customers = [];
    }
}

let cashierLineQueue = new CashierLine();
cashierLineQueue.standInQue("Customer1");
cashierLineQueue.standInQue("Customer2");
cashierLineQueue.standInQue("Customer3");
cashierLineQueue.standInQue("Customer4");
console.log(cashierLineQueue.customers);

cashierLineQueue.moveOut();
console.log("Customers in the queue: ");
console.log(cashierLineQueue.customers);
console.log("Last person in the queue: ");
console.log(cashierLineQueue.peek());
console.log("Check if the queue is empty: "+cashierLineQueue.isEmpty());

console.log("No of people in the line: "+cashierLineQueue.size());

cashierLineQueue.clear();
console.log("No of customers after the queue is cleared: "+cashierLineQueue.customers.length);

Objects

JavaScript objects are key-value pairs that represent data in a structured manner. They allow developers to store and manipulate data in a structured and organized manner. An object in JavaScript is a collection of key-value pairs, where each key represents a property of the object, and its corresponding value represents the data associated with that property.

There are two ways to create an object in JavaScript: using object literals and using the object constructor.

let person = {
  name: "John Doe",
  age: 30,
  address: {
    street: "123 Main St",
    city: "New York"
  }
};
let person = new Object();
person.name = "John Doe";
person.age = 30;
person.address = {
  street: "123 Main St",
  city: "New York"
};

In conclusion, objects are a powerful feature of JavaScript and allow developers to store and manipulate data in a structured and organized manner.