-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path031-search-array.js
More file actions
120 lines (87 loc) · 3.52 KB
/
031-search-array.js
File metadata and controls
120 lines (87 loc) · 3.52 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// 1: WHY SEARCH IN ARRAYS?
// 2: THE INCLUDES METHOD
// const fruits = ['apple', 'banana', 'orange', 'mango'];
// const hasBanana = fruits.includes('banana');
// console.log(hasBanana); // true
// const hasGrapes = fruits.includes('grapes');
// console.log(hasGrapes); // false
// const numbers = [1, 2, 3, 4, 5];
// console.log(numbers.includes(3)); // true
// console.log(numbers.includes('3')); // false - different type
// const colors = ['red', 'blue', 'green', 'blue', 'yellow'];
// console.log(colors.includes('blue')); // true
// console.log(colors.includes('blue', 3)); // true - starts from index 3
// console.log(colors.includes('blue', 4)); // false - starts from index 4
// 3: THE INDEXOF METHOD
// const animals = ['cat', 'dog', 'rabbit', 'dog', 'fish'];
// const dogIndex = animals.indexOf('dog');
// console.log(dogIndex); // 1
// const birdIndex = animals.indexOf('bird');
// console.log(birdIndex); // -1 (not found)
// const animals = ['cat', 'dog', 'rabbit', 'dog', 'fish'];
// const firstDog = animals.indexOf('dog');
// console.log(firstDog); // 1
// const secondDog = animals.indexOf('dog', 2);
// console.log(secondDog); // 3 - starts searching from index 2
// const users = ['alice', 'bob', 'charlie'];
// if (users.indexOf('david') === -1) {
// console.log('User not found');
// }
// 4: THE LASTINDEXOF METHOD
// const numbers = [5, 2, 8, 2, 9, 2];
// const lastTwo = numbers.lastIndexOf(2);
// console.log(lastTwo); // 5 (last occurrence of 2)
// const firstTwo = numbers.indexOf(2);
// console.log(firstTwo); // 1 (first occurrence of 2)
const numbers = [5, 2, 8, 2, 9, 2];
const lastTwoBeforeIndex4 = numbers.lastIndexOf(2, 4);
console.log(lastTwoBeforeIndex4); // 3
// 5: SEARCHING WITH FIND
// const products = [
// { id: 1, name: 'Laptop', price: 1000 },
// { id: 2, name: 'Phone', price: 500 },
// { id: 3, name: 'Tablet', price: 300 }
// ];
// const expensiveProduct = products.find(product => product.price > 600);
// console.log(expensiveProduct); // { id: 1, name: 'Laptop', price: 1000 }
// const users = [
// { id: 1, name: 'Alice', age: 25 },
// { id: 2, name: 'Bob', age: 30 },
// { id: 3, name: 'Charlie', age: 35 }
// ];
// const user = users.find(user => user.name === 'Bob');
// console.log(user); // { id: 2, name: 'Bob', age: 30 }
// const youngUser = users.find(user => user.age < 20);
// console.log(youngUser); // undefined (no match found)
// 6: SEARCHING WITH FINDINDEX
// const products = [
// { id: 1, name: 'Laptop', price: 1000 },
// { id: 2, name: 'Phone', price: 500 },
// { id: 3, name: 'Tablet', price: 300 }
// ];
// const index = products.findIndex(product => product.price < 400);
// console.log(index); // 2 (index of Tablet)
// const notFoundIndex = products.findIndex(product => product.price > 2000);
// console.log(notFoundIndex); // -1
// 7: PRACTICAL EXAMPLE
const cart = [
{ id: 1, name: 'Laptop', price: 1000, quantity: 1 },
{ id: 2, name: 'Mouse', price: 25, quantity: 2 },
{ id: 3, name: 'Keyboard', price: 75, quantity: 1 }
];
// Check if a product exists in cart
const hasLaptop = cart.find(item => item.name === 'Laptop');
if (hasLaptop) {
console.log('Laptop is in your cart');
}
// Find expensive items
const expensiveItems = cart.filter(item => item.price > 50);
console.log('Expensive items:', expensiveItems);
// Find the index of an item to update it
const mouseIndex = cart.findIndex(item => item.name === 'Mouse');
if (mouseIndex !== -1) {
cart[mouseIndex].quantity = 3;
console.log('Updated mouse quantity');
}
console.log(cart);
// 8: WHICH METHOD TO USE?