map
- It always returns copy of array
- sourceArray.length === outputArray.length
->
true
- Transforms data type
_7// eg:1_7const nums = [10, 5, 15];_7const squares = nums.map((num) => num * num);_7console.log(squares);_7// [ 100, 25, 225 ]_7console.log(nums);_7// [ 10, 5, 15 ]
i. Observations
nums
remains unchangednums.length equals squares.length
nums
(Array of numbers) ->squares
(Array of numbers)- Transformation was not need here
_14// eg:2_14const avengers = [_14 "Hulk",_14 "Iron man",_14 "Black widow",_14 "Captain america",_14 "Spider man",_14 "Thor",_14];_14_14// Array of Strings -> Array of numbers_14const letterCount = avengers.map((name) => name.length);_14console.log(letterCount);_14// [4, 8, 11, 15, 10, 4]
ii. Observations
avengers
remains unchangedavengers.length equals letterCount.length
nums
(Array of strings) ->squares
(Array of numbers)
filter
- It always returns copy of the array
sourceArray.length
>=outputArray.length
->true
- input data type === output data type
_5// eg:1_5const marks = [90, 40, 25, 85, 10, 0, 99];_5const passedMarks = marks.filter((mark) => mark >= 40);_5console.log(passedMarks, marks);_5// [90, 40, 85, 99]
iii. Observations
marks
remains unchangedmarks.length
is greather thanmarks.length
marks
(Array of numbers) >=passedMarks
( Array of numbers) ->true
_14// eg:2_14const avengers = [_14 "Hulk",_14 "Iron man",_14 "Black widow",_14 "Captain america",_14 "Spider man",_14 "Thor",_14];_14_14// Array of strings -> Array of strings_14const largeAvengerNames = avengers.filter((name) => name.length > 10);_14console.log(largeAvengerNames);_14// ["Black widow", "Captain america"]
iv. Observations
avengers
remains unchangedavengers.length
is greather thanlargeAvengerNames.length
avengers
(Array of strings) >=largeAvengerNames
( Array of strings) ->true
dot chaining (pattern)
- Dot chaining can continue until the array methods returns new array
_37const scores = [_37 {_37 marks: 32,_37 name: "Yvette Merritt",_37 },_37 {_37 marks: 57,_37 name: "Lillian Ellis",_37 },_37 {_37 marks: 22,_37 name: "Mccall Carter",_37 },_37 {_37 marks: 21,_37 name: "Pate Collier",_37 },_37 {_37 marks: 91,_37 name: "Debra Beard",_37 },_37 {_37 marks: 75,_37 name: "Nettie Hancock",_37 },_37 {_37 marks: 20,_37 name: "Hatfield Hodge",_37 },_37];_37_37const passedStudentsNames = scores_37 .filter((student) => student.marks >= 40)_37 .map((student) => student.name);_37_37console.log(passedStudentsNames);_37// ["Lillian Ellis", "Debra Beard", "Nettie Hancock" ]
v. Observations
filter
andmap
always returns array- Hence we could keep chaining untill array method returns
array
reduce
- Array -> any data type
_6// eg:1_6const sum = [1, 7, 3, 10, 5].reduce((acc, curr) => acc + curr, 0);_6console.log(sum); // 26_6_6const sum1 = [1, 7, 3, 10, 5].reduce((acc, curr) => acc + curr);_6console.log(sum1); // 26
initial value
-> 0
hence acc
value in first loop is 0
acc | curr | result (acc + curr) |
---|---|---|
0 | 1 | 1 |
1 | 7 | 8 |
8 | 3 | 11 |
11 | 10 | 21 |
21 | 5 | 26 |
_4// eg:2_4_4const sum1 = [1, 7, 3, 10, 5].reduce((acc, curr) => acc + curr);_4console.log(sum1); // 26
Since no initial value
hence acc
-> first value & curr
-> second value in array
acc | curr | result (acc + curr) |
---|---|---|
1 | 7 | 8 |
8 | 3 | 11 |
11 | 10 | 21 |
21 | 5 | 26 |
_7// eg:3_7const total = scores_7 .map((stu) => stu.marks)_7 .reduce((acc, curr) => acc + curr, 0);_7const avg = total / scores.length;_7console.log(total, avg.toFixed(2));_7// 318 '45.43'
@ragavkumarv
swipe to next ➡️