-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path030-array-insert.js
More file actions
84 lines (68 loc) · 2.88 KB
/
030-array-insert.js
File metadata and controls
84 lines (68 loc) · 2.88 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 1: INSERTING ELEMENTS
// 1. Using push() - Add to the End
// const fruits = ['apple', 'banana'];
// fruits.push('orange');
// console.log(fruits); // ['apple', 'banana', 'orange']
// fruits.push('mango', 'grape');
// console.log(fruits); // ['apple', 'banana', 'orange', 'mango', 'grape']
// 2. Using unshift() - Add to the Beginning
// const numbers = [2, 3, 4];
// numbers.unshift(1);
// console.log(numbers); // [1, 2, 3, 4]
// numbers.unshift(-1, 0);
// console.log(numbers); // [-1, 0, 1, 2, 3, 4]
// 3. Using splice() - Insert at Specific Position
// array.splice(startIndex, deleteCount, item1, item2, ...)
// const colors = ['red', 'blue', 'green'];
// colors.splice(1, 0, 'yellow', 'purple');
// console.log(colors); // ['red', 'yellow', 'purple', 'blue', 'green']
// 2: REMOVING ELEMENTS
// 1. Using pop() - Remove from the End
// const animals = ['cat', 'dog', 'elephant'];
// const removed = animals.pop();
// console.log(removed); // 'elephant'
// console.log(animals); // ['cat', 'dog']
// 2. Using shift() - Remove from the Beginning
// const weekdays = ['Monday', 'Tuesday', 'Wednesday'];
// const firstDay = weekdays.shift();
// console.log(firstDay); // 'Monday'
// console.log(weekdays); // ['Tuesday', 'Wednesday']
// 3. Using splice() - Remove from Specific Position
// const cities = ['New York', 'Paris', 'Tokyo', 'London'];
// cities.splice(1, 2); // Remove 2 elements starting at index 1
// console.log(cities); // ['New York', 'London']
// const months = ['Jan', 'Feb', 'Mar', 'Apr'];
// const removed = months.splice(2, 1);
// console.log(removed); // ['Mar']
// console.log(months); // ['Jan', 'Feb', 'Apr']
// 4. Using delete Operator
// const items = ['a', 'b', 'c', 'd'];
// delete items[1];
// console.log(items); // ['a', empty, 'c', 'd']
// console.log(items.length); // 4
// 3: REPLACING ELEMENTS
// 1. Direct Assignment
// const scores = [85, 90, 78, 92];
// scores[2] = 88; // Replace the element at index 2
// console.log(scores); // [85, 90, 88, 92]
// 2. Using splice() - Replace Elements
// const languages = ['Python', 'Java', 'C++', 'Ruby'];
// languages.splice(1, 2, 'JavaScript', 'TypeScript');
// console.log(languages); // ['Python', 'JavaScript', 'TypeScript', 'Ruby']
// PRACTICAL EXAMPLE
const cart = ['shirt', 'shoes', 'hat'];
// User adds a new item
cart.push('jacket');
console.log('After adding:', cart);
// User removes the first item
cart.shift();
console.log('After removing first:', cart);
// User replaces 'shoes' with 'sneakers'
const shoesIndex = cart.indexOf('shoes');
if (shoesIndex !== -1) {
cart[shoesIndex] = 'sneakers';
}
console.log('After replacing:', cart);
// User wants to insert 'socks' at position 1
cart.splice(1, 0, 'socks');
console.log('Final cart:', cart);