Skip to content

Commit 0f55399

Browse files
committed
added array methods
1 parent 975c6ce commit 0f55399

File tree

1 file changed

+103
-13
lines changed

1 file changed

+103
-13
lines changed

Array.js

Lines changed: 103 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,34 @@
22
// Array is an object (non-primitive)
33
// Array are mutable which can be directly accessed and changed
44

5-
let marks= [90, 70, 85, 24];
5+
let marks = [90, 70, 85, 24];
66
console.log(marks);
77
console.log(marks.length); //property not a method
88

9-
//**************************************************************************************/
9+
//**************************************************************************************/
1010
// Looping Over Arrays
1111

1212
// For Loop
13-
for (let i=0; i<marks.length; i++){
14-
console.log(marks[i]);
13+
for (let i = 0; i < marks.length; i++) {
14+
console.log(marks[i]);
1515
}
1616

1717
// For Of Loop
1818
for (let i of marks) {
19-
console.log(i);
19+
console.log(i);
2020
}
2121

22-
//**************************************************************************************/
22+
//**************************************************************************************/
2323
// Array Methods
24-
let foodItems= ["Potato", "Apple", "Strawberry", "Tomato"];
25-
let drinks= ["Coca Cola", "Pakola", "7UP", "Sprite"];
24+
let foodItems = ["Potato", "Apple", "Strawberry", "Tomato"];
25+
let drinks = ["Coca Cola", "Pakola", "7UP", "Sprite"];
2626

2727
//Push() adds the item at the end of an original array
2828
foodItems.push("Chips", "Burgers");
2929
console.log(foodItems);
3030

3131
// Pop() removes the item from the end of an original array & returns it
32-
let deleted= foodItems.pop();
32+
let deleted = foodItems.pop();
3333
console.log(`Deleted Item ${deleted}`);
3434
console.log(foodItems);
3535

@@ -43,16 +43,16 @@ console.log(foodItems.concat(drinks));
4343
foodItems.unshift("Pasta");
4444

4545
// Shift() Removes Item from the start of an array & returns
46-
let sDel= foodItems.shift();
46+
let sDel = foodItems.shift();
4747
console.log(sDel);
4848

4949
// Slice() Returns a piece of the array slice(startidx, endidx)
50-
console.log(foodItems.slice(1,3));
50+
console.log(foodItems.slice(1, 3));
5151

5252
// Splice() Change original array (add, remove, replace) splice(startidx, delCount, newEl1)
5353
// 2nd index sy start krky 3 elements delete krny hain and us 2nd index pai "jam" insert krna hai
5454
console.log("\n", foodItems);
55-
foodItems.splice(2,3,"jam");
55+
foodItems.splice(2, 3, "jam");
5656
console.log("\n", foodItems);
5757

5858
// Add Element
@@ -65,4 +65,94 @@ console.log("\n", foodItems);
6565

6666
// Replace Element
6767
foodItems.splice(1, 1, "cookie");
68-
console.log("\n", foodItems);
68+
console.log("\n", foodItems);
69+
70+
//**************************************************************************************/
71+
//WHAT ARE HIGHER ORDER FUNCTIONS / METHODS (HOD/HOM) ????????????//
72+
/*
73+
HOD takes functions either as parameters or returns a function as a value
74+
Such as: forEach is a HOD function
75+
*/
76+
77+
//**************************************************************************************/
78+
//ForEach Loop in Arrays
79+
80+
let arr = [1, 2, 3, 4, 5, 6, 7, 8];
81+
82+
//forEach will take each array element as a value here.
83+
//forEach automatically executes the function, we do not need to do it.
84+
arr.forEach(function printValue(val) {
85+
console.log(val);
86+
});
87+
88+
let cities = ["Lahore", "Faisalabad", "Karachi", "Hyderabad"];
89+
cities.forEach((val) => {
90+
console.log(val.toUpperCase());
91+
});
92+
93+
/*FOR EACH CAN TAKE 3 PARAMATERS i.e. value, index, and array (optional)*/
94+
cities.forEach((val, idx, arr) => {
95+
console.log(val.toUpperCase(), idx, arr);
96+
});
97+
98+
//**************************************************************************************/
99+
// MAP in Arrays
100+
// Map and ForEach are very similar to each other. They both are utilized to access each element of an array. The only difference is map returns a new array, but forEach() does not.
101+
// forEach() is used to perform simple calculations, while map() is used when we need a new array with some specific operation performed on it.
102+
103+
arr.map((val) => {
104+
console.log(val);
105+
});
106+
107+
let calSq = arr.map((val) => {
108+
return val * val;
109+
});
110+
111+
console.log(calSq);
112+
113+
//**************************************************************************************/
114+
/* FILTER METHOD
115+
Creates a new array of values that give true for a specified condition or filter
116+
*/
117+
let even = arr.filter((val) => {
118+
return val % 2 == 0;
119+
});
120+
console.log(even);
121+
122+
let odd = arr.filter((val) => {
123+
return val % 2 !== 0;
124+
});
125+
console.log(odd);
126+
127+
//**************************************************************************************/
128+
/* REDUCE METHOD
129+
Performs some operations and reduce the array to a single. It return that single value.
130+
131+
It works like this:
132+
for suppose let arr= [1,2,3,4]
133+
const output= arr.reduce((res, curr)=>{
134+
return res+curr;
135+
});
136+
137+
1st iteration
138+
res=1, curr= 2 ---> after execution res=3
139+
2nd iteration
140+
res= 3, curr= 3 ---> after execution res=6
141+
3rd iteration
142+
res= 6, curr= 4 ---> after execution res=10
143+
144+
Output will be 10
145+
*/
146+
147+
const output = arr.reduce((res, curr) => {
148+
return res + curr;
149+
});
150+
151+
console.log(output);
152+
153+
// Find the largest element in the array
154+
const largest = arr.reduce((prev, curr) => {
155+
return prev > curr ? prev : curr;
156+
});
157+
158+
console.log(largest);

0 commit comments

Comments
 (0)