-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_spliceArrayMethod.js
More file actions
65 lines (55 loc) · 2.67 KB
/
Copy path6_spliceArrayMethod.js
File metadata and controls
65 lines (55 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// JavaScript Array splice() Method Examples
// 1. Basic splice() usage - Removing elements
const numbers = [1, 2, 3, 4, 5, 6];
const removed = numbers.splice(2, 2); // Removes 2 elements starting from index 2
console.log("Modified Numbers:", numbers); // Output: [1, 2, 5, 6]
console.log("Removed Elements:", removed); // Output: [3, 4]
// 2. Using splice() to insert elements
const fruits = ["Apple", "Banana", "Cherry"];
fruits.splice(1, 0, "Blueberry", "Mango"); // Insert at index 1 without removing any element
console.log("Fruits after insertion:", fruits);
// Output: ["Apple", "Blueberry", "Mango", "Banana", "Cherry"]
// 3. Using splice() to replace elements
const colors = ["Red", "Green", "Blue"];
colors.splice(1, 1, "Yellow"); // Replace element at index 1
console.log("Colors after replacement:", colors);
// Output: ["Red", "Yellow", "Blue"]
// 4. Removing all elements from a specific index using splice()
const animals = ["Dog", "Cat", "Elephant", "Lion", "Tiger"];
animals.splice(2); // Removes all elements from index 2 onwards
console.log("Animals after removal:", animals);
// Output: ["Dog", "Cat"]
// 5. Using splice() to remove elements without leaving gaps
const numbers2 = [10, 20, 30, 40, 50];
numbers2.splice(1, 2); // Removes 2 elements from index 1
console.log("Numbers after removal:", numbers2);
// Output: [10, 40, 50]
// 6. Using splice() to insert elements at the end
const letters = ["A", "B", "C"];
letters.splice(letters.length, 0, "D", "E"); // Appending at the end
console.log("Letters after append:", letters);
// Output: ["A", "B", "C", "D", "E"]
// 7. Using splice() to extract a subarray (modifying the original)
const nums = [1, 2, 3, 4, 5, 6];
const extracted = nums.splice(2, 3); // Removes 3 elements from index 2
console.log("Extracted Elements:", extracted); // Output: [3, 4, 5]
console.log("Remaining Numbers:", nums); // Output: [1, 2, 6]
// 8. Using splice() on an empty array (No effect)
const emptyArray = [];
emptyArray.splice(0, 2, "Inserted");
console.log("Empty Array after splice:", emptyArray);
// Output: ["Inserted"]
// 9. Using splice() to delete elements without replacement
const courses = ["Math", "Science", "History", "Geography"];
courses.splice(1, 2); // Removes "Science" and "History"
console.log("Courses after removal:", courses);
// Output: ["Math", "Geography"]
// 10. Using splice() to manipulate elements dynamically
function removeLastTwoElements(arr) {
return arr.splice(-2); // Removes last two elements
}
const items = ["Pen", "Pencil", "Eraser", "Sharpener"];
console.log("Removed Items:", removeLastTwoElements(items));
// Output: ["Eraser", "Sharpener"]
console.log("Remaining Items:", items);
// Output: ["Pen", "Pencil"]