Repository navigation
Expand file tree
/
Copy path15_important_question.js
More file actions
70 lines (49 loc) · 2.15 KB
/
Copy path15_important_question.js
File metadata and controls
70 lines (49 loc) · 2.15 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
/**
* JavaScript Array Methods — 10 Important Examples
* Topic: Most commonly used array methods (interview favorites)
*/
// 1. map() — har element ko transform karo
const nums1 = [1, 2, 3, 4];
const doubled = nums1.map(n => n * 2);
console.log("1. map():", doubled); // [2, 4, 6, 8]
// 2. filter() — condition ke basis pe elements chuno
const nums2 = [1, 2, 3, 4, 5, 6];
const evens = nums2.filter(n => n % 2 === 0);
console.log("2. filter():", evens); // [2, 4, 6]
// 3. reduce() — sab kuch ek value mein combine karo
const prices = [100, 200, 300];
const total = prices.reduce((sum, price) => sum + price, 0);
console.log("3. reduce():", total); // 600
// 4. forEach() — bas loop chalao, kuch return nahi karna
const fruits = ["apple", "mango", "banana"];
fruits.forEach(fruit => console.log("4. forEach():", fruit));
// 5. find() — pehla matching element dhoondo
const users = [{ id: 1, name: "Aman" }, { id: 2, name: "Riya" }];
const user = users.find(u => u.id === 2);
console.log("5. find():", user); // {id: 2, name: "Riya"}
// 6. some() — check karo koi ek condition satisfy karta hai kya
const nums3 = [1, 3, 5, 8];
const hasEven = nums3.some(n => n % 2 === 0);
console.log("6. some():", hasEven); // true
// 7. every() — check karo SAB elements condition satisfy karte hain
const ages = [22, 25, 19, 30];
const allAdults = ages.every(age => age >= 18);
console.log("7. every():", allAdults); // true
// 8. sort() — array ko order mein lagao
const nums4 = [5, 2, 9, 1];
nums4.sort((a, b) => a - b); // ascending
console.log("8. sort():", nums4); // [1, 2, 5, 9]
// 9. includes() — element hai ya nahi, quick check
const skills = ["React", "Node", "MongoDB"];
console.log("9. includes():", skills.includes("Node")); // true
// 10. Chaining — sab methods ko combine karke real-world use
const products = [
{ name: "Laptop", price: 50000, inStock: true },
{ name: "Phone", price: 20000, inStock: false },
{ name: "Tablet", price: 15000, inStock: true },
];
const totalAvailableValue = products
.filter(p => p.inStock)
.map(p => p.price)
.reduce((sum, price) => sum + price, 0);
console.log("10. Chaining:", totalAvailableValue); // 65000