1Map, Filter, Reduce javascript
2//=================================
3let myarray = [
4 {
5 name: "John",
6 city: "london",
7 dept: "computer",
8 salary: 1000,
9 },
10 {
11 name: "bella",
12 city: "new york",
13 dept: "fianance",
14 salary: 1000,
15 },
16 {
17 name: "justine",
18 city: "paris",
19 dept: "computer",
20 salary: 1000,
21 },
22];
23console.log("myarray : ", myarray);
24// ----------------------------------
25// map
26let mapArray = myarray.map(function (item) {
27 return item.name;
28});
29console.log("mapArray : ", mapArray);
30// ----------------------------------
31//filter
32let filterArray = myarray.filter(function (item) {
33 if (item.dept == "computer") {
34 return true;
35 } else {
36 return false;
37 }
38});
39console.log("filterArray : ", filterArray);
40// ----------------------------------
41//reduce
42// 1.
43let reduceArray1 = myarray.reduce(function (acc, item) {
44 return acc + item.salary;
45}, 0);
46console.log("reduceArray1 : ", reduceArray1);
47// ------
48// 2.
49let reduceArray2 = myarray.reduce(
50 function (acc, item) {
51 if (item.dept == "computer") {
52 acc.com.push(item.name);
53 } else {
54 acc.finance.push(item.name);
55 }
56 return acc;
57 },
58 { com: [], finance: [] }
59);
60console.log("reduceArray2 : ", reduceArray2);
61// ----------------------------------
1const rebels = pilots.filter(pilot => pilot.faction === "Rebels");const empire = pilots.filter(pilot => pilot.faction === "Empire");
1const numbers = [1, 2, 3, 4];
2const sum = numbers.reduce(function (result, item) {
3 return result + item;
4}, 0);
5console.log(sum); // 10
1const students = [
2 { name: 'Quincy', grade: 96 },
3 { name: 'Jason', grade: 84 },
4 { name: 'Alexis', grade: 100 },
5 { name: 'Sam', grade: 65 },
6 { name: 'Katie', grade: 90 }
7];
8
9const studentGrades = students.filter(student => student.grade >= 90);
10return studentGrades; // [ { name: 'Quincy', grade: 96 }, { name: 'Alexis', grade: 100 }, { name: 'Katie', grade: 90 } ]