Skip to content

Commit bee0378

Browse files
committed
filter reduce map other fun
1 parent 769df03 commit bee0378

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

Programs/arrayfun.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
2+
3+
/*
4+
5+
6+
let numbers=new Array(1,2,3,4)
7+
console.log(numbers)// [ 1, 2, 3, 4 ]
8+
9+
10+
11+
Push
12+
-----
13+
14+
// Adds elements to the end of an array.
15+
let arr=[1,2,3,4]
16+
arr.push(5,6)
17+
console.log(arr)// [ 1, 2, 3, 4, 5, 6 ]
18+
19+
20+
POP
21+
____
22+
23+
24+
//Removes the last element and returns it.
25+
26+
27+
let arr=[1,2,3,4,5,6]
28+
arr.pop()
29+
30+
console.log(arr)// [ 1, 2, 3, 4, 5 ]
31+
32+
Shift
33+
______
34+
35+
//Removes the first element and returns it.
36+
37+
38+
let arr=[1,2,3,4,5,6]
39+
arr.shift()
40+
console.log(arr)// [ 2, 3, 4, 5, 6 ]
41+
42+
43+
Unshift()
44+
----------
45+
46+
//Adds elements to the beginning of an array.
47+
48+
49+
let arr=[2,3,5]
50+
arr.unshift(6)
51+
console.log(arr)//[ 6, 2, 3, 5 ]
52+
53+
54+
*/
55+
56+
57+
/*
58+
59+
MAP :- it is a built in js array method it creates a new array calling function
60+
let arr=[1,2,3,4]
61+
let newarr=arr.map(num=>num>2)
62+
console.log(newarr)//based on condition true false
63+
64+
65+
66+
let arr=[1,2,3,4]
67+
let newarr=arr.map(num=>num*2)
68+
console.log(newarr)//[2,4,6,8]
69+
*/
70+
71+
72+
/*
73+
74+
Filter - It Creates a new array based on the codition
75+
76+
let arr=[1,2,3,4]
77+
let newarr=arr.filter(num=>num>2)
78+
console.log(newarr)// [3,4]
79+
80+
81+
82+
let arr=[1,2,3,4]
83+
let newarr=arr.filter(num=>num%2==0)
84+
console.log(newarr)//[2,4]
85+
*/
86+
87+
88+
/*
89+
90+
Reduce -It is a built in array method .It is used to reduced the array in a single value
91+
92+
93+
let arr=[1,2,3,4]
94+
let newarr=arr.reduce((acc,num)=>acc+num)
95+
console.log(newarr)//10
96+
97+
98+
let arr=[1,2,3,4]
99+
let newarr=arr.reduce((acc,num)=>acc*num)
100+
console.log(newarr)//24
101+
*/
102+
103+
104+
105+
/*
106+
107+
🔍 find() vs filter()
108+
Feature find() filter()
109+
Purpose Finds the first element that matches a condition Filters all elements that match a condition
110+
Returns A single element (or undefined) A new array of all matching elements
111+
Stops At First match Checks every item
112+
Return Type Value (element) Array
113+
114+
115+
116+
117+
const nums = [1, 3, 5, 7];
118+
const result = nums.find(num => num > 4);
119+
console.log(result); // Output: 5
120+
121+
*/

0 commit comments

Comments
 (0)