Skip to content

Commit

Permalink
feat(array-methods): add examples for array manipulation methods
Browse files Browse the repository at this point in the history
- Demonstrated various array methods including push, pop, unshift, shift, slice, and splice with detailed examples.

- Included examples showcasing:
  - Adding and removing elements dynamically using push and pop.
  - Array conversions using  	oString.
  - Merging arrays with concat.
  - Replacing and modifying elements using splice.

- Added a practice scenario involving company names:
  - Removed the first company.
  - Replaced Uber with Ola.
  - Added Amazon at the end.
  • Loading branch information
Harshitap0809 committed Nov 27, 2024
1 parent 43bd158 commit adc1452
Showing 1 changed file with 61 additions and 47 deletions.
108 changes: 61 additions & 47 deletions 02_Basics/01_Arrays8.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// ==========================
// Array Methods Demonstration
// ==========================
Expand All @@ -9,185 +8,179 @@

// Initialize an array with food items
let foodArray = ["chips", "cheese", "bread", "paneer"];
console.log("Initial foodArray:", foodArray);
console.log("Initial foodArray:", foodArray);
// Output: Initial foodArray: [ 'chips', 'cheese', 'bread', 'paneer' ]

// Mutates the original array by adding items
foodArray.push("milk", "onion", "corn-flour");
console.log("After push:", foodArray);
console.log("After push:", foodArray);
// Output: After push: [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour' ]

// Returns the new length of the array
let updatedLength = foodArray.push("lettuce");
console.log("New length:", updatedLength);
console.log("New length:", updatedLength);
// Output: New length: 8

// Can accept multiple arguments
foodArray.push("tomato", "cucumber");
console.log("After pushing more items:", foodArray);
console.log("After pushing more items:", foodArray);
// Output: After pushing more items: [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour', 'lettuce', 'tomato', 'cucumber' ]

// Handles different data types
foodArray.push(42, { item: "apple" });
console.log("After pushing different types:", foodArray);
// Output: After pushing different types:
// [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour',
console.log("After pushing different types:", foodArray);
// Output: After pushing different types:
// [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour',
// 'lettuce', 'tomato', 'cucumber', 42, { item: 'apple' } ]

// Can push `undefined`
foodArray.push(undefined);
console.log("After pushing undefined:", foodArray);
// Output: After pushing undefined:
// [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour',
console.log("After pushing undefined:", foodArray);
// Output: After pushing undefined:
// [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour',
// 'lettuce', 'tomato', 'cucumber', 42, { item: 'apple' }, undefined ]

// Use in loops to add items
let additionalItems = ["carrot", "spinach"];
additionalItems.forEach(item => foodArray.push(item));
console.log("After pushing items in a loop:", foodArray);
// Output: After pushing items in a loop:
// [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour',
// 'lettuce', 'tomato', 'cucumber', 42, { item: 'apple' }, undefined,
additionalItems.forEach((item) => foodArray.push(item));
console.log("After pushing items in a loop:", foodArray);
// Output: After pushing items in a loop:
// [ 'chips', 'cheese', 'bread', 'paneer', 'milk', 'onion', 'corn-flour',
// 'lettuce', 'tomato', 'cucumber', 42, { item: 'apple' }, undefined,
// 'carrot', 'spinach' ]


// ==========================
// 2. pop() : Delete from end & return
// ==========================

// Initialize an array of fruits
let fruitArray = ["apple", "banana"];
console.log("Initial fruitArray:", fruitArray);
console.log("Initial fruitArray:", fruitArray);
// Output: Initial fruitArray: [ 'apple', 'banana' ]

// Use push() to add a single element
fruitArray.push("orange");
console.log("After push('orange'):", fruitArray);
console.log("After push('orange'):", fruitArray);
// Output: After push('orange'): [ 'apple', 'banana', 'orange' ]

// Use push() to add multiple elements
fruitArray.push("grape", "kiwi");
console.log("After push('grape', 'kiwi'):", fruitArray);
console.log("After push('grape', 'kiwi'):", fruitArray);
// Output: After push('grape', 'kiwi'): [ 'apple', 'banana', 'orange', 'grape', 'kiwi' ]

// Pushing different data types
let mixedArray = [1, "two", true];
mixedArray.push(null, { key: "value" });
console.log("Mixed array after push:", mixedArray);
console.log("Mixed array after push:", mixedArray);
// Output: Mixed array after push: [ 1, 'two', true, null, { key: 'value' } ]

// Push on an empty array
let emptyArray = [];
emptyArray.push(1);
console.log("Empty array after push(1):", emptyArray);
console.log("Empty array after push(1):", emptyArray);
// Output: Empty array after push(1): [ 1 ]


// ==========================
// 3. toString() : Converts array to string
// ==========================

// Initialize an array of colors
let colorArray = ["red", "green", "blue"];
let colorString = colorArray.toString();
console.log("Array as string:", colorString);
console.log("Array as string:", colorString);
// Output: Array as string: red,green,blue

// Handle nested arrays
let nestedArray = [1, 2, [3, 4], 5];
console.log("Nested array as string:", nestedArray.toString());
console.log("Nested array as string:", nestedArray.toString());
// Output: Nested array as string: 1,2,3



// ==========================
// 4. concat() : Merge Arrays
// ==========================

// Initialize two arrays
let firstArray = ["a", "b", "c"];
let secondArray = ["d", "e", "f"];
console.log("Initial firstArray:", firstArray);
console.log("Initial firstArray:", firstArray);
// Output: Initial firstArray: [ 'a', 'b', 'c' ]
console.log("Initial secondArray:", secondArray);
console.log("Initial secondArray:", secondArray);
// Output: Initial secondArray: [ 'd', 'e', 'f' ]

// Use concat() to merge arrays
let mergedArray = firstArray.concat(secondArray);
console.log("Merged array:", mergedArray);
console.log("Merged array:", mergedArray);
// Output: Merged array: [ 'a', 'b', 'c', 'd', 'e', 'f' ]

// Concat with multiple arrays
let thirdArray = ["g", "h"];
let combinedArray = firstArray.concat(secondArray, thirdArray);
console.log("Combined array:", combinedArray);
console.log("Combined array:", combinedArray);
// Output: Combined array: [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ]

// Concat with different data types
let mixedConcatArray = firstArray.concat(42, { key: "value" });
console.log("Mixed array after concat:", mixedConcatArray);
console.log("Mixed array after concat:", mixedConcatArray);
// Output: Mixed array after concat: [ 'a', 'b', 'c', 42, { key: 'value' } ]


// ==========================
// 5. unshift() : Add to Beginning
// ==========================

// Initialize an array of numbers
let numberArray = [2, 3, 4];
console.log("Initial numberArray:", numberArray);
console.log("Initial numberArray:", numberArray);
// Output: Initial numberArray: [ 2, 3, 4 ]

// Use unshift() to add elements to the beginning
numberArray.unshift(1);
console.log("After unshift(1):", numberArray);
console.log("After unshift(1):", numberArray);
// Output: After unshift(1): [ 1, 2, 3, 4 ]

// Unshift multiple elements
numberArray.unshift(-1, 0);
console.log("After unshift(-1, 0):", numberArray);
console.log("After unshift(-1, 0):", numberArray);
// Output: After unshift(-1, 0): [ -1, 0, 1, 2, 3, 4 ]

// Unshift different data types
numberArray.unshift("start", null);
console.log("After unshift('start', null):", numberArray);
console.log("After unshift('start', null):", numberArray);
// Output: After unshift('start', null): [ 'start', null, -1, 0, 1, 2, 3, 4 ]


// ==========================
// 6. shift() : Remove from Beginning & Return
// ==========================

// Initialize an array of fruits
let fruitList = ["apple", "banana", "cherry"];
console.log("Initial fruitList:", fruitList);
console.log("Initial fruitList:", fruitList);
// Output: Initial fruitList: [ 'apple', 'banana', 'cherry' ]

// Use shift() to remove the first element
let removedFruit = fruitList.shift();
console.log("Removed fruit:", removedFruit);
console.log("Removed fruit:", removedFruit);
// Output: Removed fruit: apple
console.log("After shift:", fruitList);
console.log("After shift:", fruitList);
// Output: After shift: [ 'banana', 'cherry' ]

// Shift again
let secondRemovedFruit = fruitList.shift();
console.log("Removed fruit:", secondRemovedFruit);
console.log("Removed fruit:", secondRemovedFruit);
// Output: Removed fruit: banana
console.log("After another shift:", fruitList);
console.log("After another shift:", fruitList);
// Output: After another shift: [ 'cherry' ]

// Shift from a single-element array
fruitList.shift();
console.log("After shifting the last element:", fruitList);
console.log("After shifting the last element:", fruitList);
// Output: After shifting the last element: [ ]

// Shift from an empty array
let emptyList = [];
let removedFromEmptyList = emptyList.shift();
console.log("Removed from empty array:", removedFromEmptyList);
console.log("Removed from empty array:", removedFromEmptyList);
// Output: Removed from empty array: undefined
console.log("After shift on empty array:", emptyList);
console.log("After shift on empty array:", emptyList);
// Output: After shift on empty array: [ ]

// ==========================
Expand Down Expand Up @@ -219,7 +212,6 @@ console.log("Sliced with negative indices (-3, -1):", slicedNegative);
console.log("After slice, original array:", vegArray);
// Output: After slice, original array: [ 'carrot', 'potato', 'tomato', 'onion', 'spinach' ]


// ==========================
// 8. splice() : Modify original array (add, remove, or replace)
// syntax : splice( startIdx, delCount, newElm...)
Expand Down Expand Up @@ -251,3 +243,25 @@ console.log("After replacing an element:", fruitBasket);
fruitBasket.splice(0, fruitBasket.length);
console.log("After clearing all elements:", fruitBasket);
// Output: After clearing all elements: []

//----------------------------------------------
// Let's Practice
//----------------------------------------------
// 1) Create an array to store companies -> "Bloomberg","Microsoft","Uber","Google","IBM","Netflix"
// a. Remove the first company from the array.
// b. Remove Uber & add Ola in it's place.
// c. Add Amazon at the end.

// a
let companies = ["Bloomberg", "Microsoft", "Uber", "Google", "IBM", "Netflix"];
companies.shift();
console.log(companies);

// b
companies.splice(1, 1, "Ola");
console.log(companies);

// c
companies.push("Amazon");
console.log(companies);
// ------------------------------------end-------------------------------------------------

0 comments on commit adc1452

Please sign in to comment.