Skip to content

Commit 21c9bd3

Browse files
map.filter and reduce
1 parent f4945a4 commit 21c9bd3

File tree

4 files changed

+225
-2
lines changed

4 files changed

+225
-2
lines changed

MapFilterReduce.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
let arr = [5, 1, 3, 6, 2];
2+
3+
// function double(i){
4+
// return i*2;
5+
// }
6+
// function triple(i){
7+
// return i*3;
8+
// }
9+
// function binary(i){
10+
// return i.toString(2);//to convert number into binary
11+
// }
12+
// let output=arr.map(double);
13+
console.log(arr.map(function double(i) { ///1way
14+
return i * 2;
15+
}));
16+
console.log(arr.map(function (i) {
17+
return i * 3;
18+
}));
19+
console.log(arr.map((i) => i.toString(2)));//finak efficinet way of writting map
20+
21+
22+
console.log("=============================filter===========================");
23+
function isOdd(i) {
24+
return i % 2;
25+
}
26+
function isEven(i) {
27+
return i % 2;
28+
}
29+
let output = arr.filter(isOdd);
30+
console.log(arr.filter(function isOdd(i) {
31+
return i % 2;//odd
32+
}));
33+
console.log(arr.filter((i) => {
34+
return i % 2 == 0;//even
35+
}));
36+
console.log(arr.filter((i) => i % 2 !== 0));//not even
37+
38+
console.log("========================reduce===========================");
39+
function sum(acc, curr) {
40+
acc += curr;
41+
return acc;
42+
}
43+
44+
console.log(arr.reduce(sum, 0));
45+
console.log(arr.reduce(function sum(acc, curr) {
46+
acc += curr;
47+
return acc;
48+
}, 0));
49+
console.log(arr.reduce(function max(acc, curr) {
50+
if (acc < curr) {
51+
acc = curr;
52+
}
53+
return acc;
54+
55+
}, 0));
56+
console.log(arr.reduce((acc, curr)=> {
57+
if (acc < curr) {
58+
acc = curr;
59+
}
60+
return acc;
61+
62+
}, 0));
63+
64+
65+

MapFilterReducev2.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
let arr = [{first: "Swathi", lastName: "Amaravadi", age: "27"},
2+
{ first: "Rajesh", lastName: "kodakandla", age: "33" },
3+
{ first: "gopireddy", lastName: "Amaravadi", age: "25" },
4+
{ first: "Anand", lastName: "yannamaneni", age: "27" }]
5+
6+
// [{"swathi Amaravadi"}]
7+
// as we need to get transformation of input for all input objects so in that case should use Map
8+
9+
let output1 = arr.map((x) => x.first + " " + x.lastName);
10+
console.log(output1);
11+
12+
13+
//here we need to get a output of object that contains all age entries in the array and frequencyies of it so as we are not transforming input array and not filtering from that juist forming new output from given input
14+
15+
let output2 = arr.reduce((acc, curr) => {
16+
if (acc[curr.age]) {
17+
acc[curr.age]++;
18+
} else {
19+
acc[curr.age] = 1;
20+
}
21+
return acc;
22+
}, {});
23+
console.log(output2);
24+
25+
//here need to get only lastnames of whose age is greater than 30
26+
let output3 = arr.filter((x) => x.age > 30).map((x) => x.lastName);
27+
console.log(output3);
28+
29+
30+
let output = arr.reduce(function (acc, curr) {
31+
if (curr.age > 30) {
32+
acc.push(curr.lastName);
33+
}
34+
return acc;
35+
}, [])
36+
console.log(output);

README.md

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,4 +791,126 @@ https://claude.ai/public/artifacts/29027752-4a20-451f-a5a0-743050848a4b
791791
3.call back function a function that passed trhough argument fir another function
792792
793793
794-
here call back function passed as argument to huigher order function its abiloity is called first class function
794+
here call back function passed as argument to huigher order function its abiloity is called first class function
795+
796+
797+
Day-8:
798+
799+
// Sample array
800+
let arr = [5, 1, 3, 6, 2];
801+
802+
// ========================== MAP ==========================
803+
console.log("================= MAP =================");
804+
// MAP: Creates a new array by applying a function to each element
805+
806+
// Method 1: Named function declaration outside map
807+
function double(i) {
808+
return i * 2;
809+
}
810+
console.log("Double:", arr.map(double));
811+
812+
// Method 2: Anonymous function inside map
813+
console.log("Triple:", arr.map(function(i) {
814+
return i * 3;
815+
}));
816+
817+
// Method 3: Arrow function (most concise)
818+
console.log("Binary:", arr.map(i => i.toString(2)));
819+
820+
821+
// ========================== FILTER ==========================
822+
console.log("\n================= FILTER =================");
823+
// FILTER: Creates a new array with elements that pass a test
824+
825+
// Method 1: Named function declaration
826+
function isOdd(i) {
827+
return i % 2; // Non-zero values are truthy (odd numbers)
828+
}
829+
console.log("Odd numbers:", arr.filter(isOdd));
830+
831+
// Method 2: Anonymous function
832+
console.log("Even numbers:", arr.filter(function(i) {
833+
return i % 2 === 0;
834+
}));
835+
836+
// Method 3: Arrow function
837+
console.log("Odd numbers (arrow):", arr.filter(i => i % 2 !== 0));
838+
839+
840+
// ========================== REDUCE ==========================
841+
console.log("\n================= REDUCE =================");
842+
// REDUCE: Accumulates array values into a single result
843+
844+
// Method 1: Named function declaration
845+
function sum(acc, curr) {
846+
acc += curr;
847+
return acc;
848+
}
849+
console.log("Sum:", arr.reduce(sum, 0));
850+
851+
// Method 2: Anonymous function
852+
console.log("Sum (anonymous):", arr.reduce(function(acc, curr) {
853+
acc += curr;
854+
return acc;
855+
}, 0));
856+
857+
// Method 3: Finding maximum value
858+
console.log("Max value:", arr.reduce(function(acc, curr) {
859+
if (acc < curr) {
860+
acc = curr;
861+
}
862+
return acc;
863+
}, 0));
864+
865+
// Method 4: Max with arrow function
866+
console.log("Max (arrow):", arr.reduce((acc, curr) => {
867+
if (acc < curr) {
868+
acc = curr;
869+
}
870+
return acc;
871+
}, 0));
872+
873+
874+
// ========================== PRACTICAL EXAMPLES ==========================
875+
console.log("\n================= PRACTICAL EXAMPLES =================");
876+
877+
// Array of objects
878+
let people = [
879+
{first: "Swathi", lastName: "Amaravadi", age: "27"},
880+
{first: "Rajesh", lastName: "Kodakandla", age: "33"},
881+
{first: "Gopireddy", lastName: "Amaravadi", age: "25"},
882+
{first: "Anand", lastName: "Yannamaneni", age: "27"}
883+
];
884+
885+
// Example 1: Map - Create full names array
886+
let fullNames = people.map(person => person.first + " " + person.lastName);
887+
console.log("Full names:", fullNames);
888+
889+
// Example 2: Reduce - Count occurrences of each age
890+
let ageFrequency = people.reduce((acc, curr) => {
891+
if (acc[curr.age]) {
892+
acc[curr.age]++;
893+
} else {
894+
acc[curr.age] = 1;
895+
}
896+
return acc;
897+
}, {});
898+
console.log("Age frequency:", ageFrequency);
899+
900+
// Example 3: Chaining methods - Get lastNames of people over 30
901+
let over30LastNames = people
902+
.filter(person => person.age > 30)
903+
.map(person => person.lastName);
904+
console.log("LastNames of people over 30:", over30LastNames);
905+
906+
// Example 4: Alternative using reduce for the same operation
907+
let over30LastNamesAlt = people.reduce((acc, curr) => {
908+
if (curr.age > 30) {
909+
acc.push(curr.lastName);
910+
}
911+
return acc;
912+
}, []);
913+
console.log("LastNames of people over 30 (with reduce):", over30LastNamesAlt);
914+
915+
916+
https://claude.ai/public/artifacts/864c8182-bd9e-4d29-827a-2e21653dbf10

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<script src="./LeetCode-counter-ii"></script>
4+
<script src="./MapFilterReducev2.js"></script>
55
</head>
66
</html>

0 commit comments

Comments
 (0)