ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • forEach & filter & map & sort & reduce
    JavaScript 2021. 6. 11. 01:48

     

     

    const companies = [
        {name: "Company One", category: "Finance", start: 1981, end: 2003},
        {name: "Company Two", category: "Retail", start: 1992, end: 2008},
        {name: "Company Three", category: "Auto", start: 1999, end: 2007},
        {name: "Company Four", category: "Retail", start: 1985, end: 2010},
        {name: "Company Five", category: "Technology", start: 2009, end: 2017},
        {name: "Company Six", category: "Finance", start: 2000, end: 2015},
        {name: "Company Seven", category: "Auto", start: 1986, end: 1998},
        {name: "Company Eight", category: "Technology", start: 2011, end: 2018},
        {name: "Company Nine", category: "Retail", start: 1980, end: 1989}
    ];
    
    const ages = [33, 12, 20, 16, 5, 54, 21, 44, 61, 13, 15, 45, 25, 64, 32];
    
    

     

     

    1. forEach

    // basic for
    for(let i = 0; i < companies.length; i++){
        console.log(companies[i]);
    }
    
    // forEach
    companies.forEach(function(company) {
        console.log(company.name);
    })
    
    /*
    Company One
    Company Two
    Company Three
    Company Four
    Company Five
    Company Six
    Company Seven
    Company Eight
    Company Nine
    */

     

     

    2. filter

    const canDrink2 = ages.filter(function(age){
        if(age >= 21){
            return true;
        }
    })
    console.log(canDrink2);
    // [ 33, 54, 21, 44, 61, 45, 25, 64, 32] 
    
    // more clean code
    const canDrink3 = ages.filter(age => age >= 21);
    console.log(canDrink3);
    
    
    // Filter retail companies
    const retailCompanies = companies.filter(function(company){
        if(company.category === 'Retail'){
            return true;
        }
    })
    console.log(retailCompanies);
    /*
    [
      { name: 'Company Two', category: 'Retail', start: 1992, end: 2008 },
      { name: 'Company Four', category: 'Retail', start: 1985, end: 2010 },
      { name: 'Company Nine', category: 'Retail', start: 1980, end: 1989 }
    ]
    */
    
    // more clean code
    const retailCompanies2 = companies.filter(company => company.category === 'Retail');
    console.log(retailCompanies2);
    
    
    // Get 80s companies
    const eightiesCompanies = companies.filter(company => (company.start >= 1980 && company.start < 1990));
    console.log(eightiesCompanies);
    /*
    [
      { name: 'Company One', category: 'Finance', start: 1981, end: 2003 },
      { name: 'Company Four', category: 'Retail', start: 1985, end: 2010 },
      { name: 'Company Seven', category: 'Auto', start: 1986, end: 1998 },
      { name: 'Company Nine', category: 'Retail', start: 1980, end: 1989 }
    ]
    */
    
    // Get companies that lasted 10 years or more
    const lastedTenYears = companies.filter(company => (company.end - company.start >= 10));
    console.log(lastedTenYears);
    /*
    [
      { name: 'Company One', category: 'Finance', start: 1981, end: 2003 },
      { name: 'Company Two', category: 'Retail', start: 1992, end: 2008 },
      { name: 'Company Four', category: 'Retail', start: 1985, end: 2010 },
      { name: 'Company Six', category: 'Finance', start: 2000, end: 2015 },
      { name: 'Company Seven', category: 'Auto', start: 1986, end: 1998 }
    ] 
    */

     

     

    3. map

    // Create array of company names
    const companyNames = companies.map(function(company){
        return company.name;
    })
    console.log(companyNames);
    
    // more clean code
    const companyNames2 = companies.map(company => company.name)
    console.log(companyNames2);
    
    
    const testMap = companies.map(
                            company => 
                            `${company.name} [${company.start} - ${company.end}]`);
    console.log(testMap);
    /*
    [
      'Company One [1981 - 2003]',
      'Company Two [1992 - 2008]',
      'Company Three [1999 - 2007]',
      'Company Four [1985 - 2010]',
      'Company Five [2009 - 2017]',
      'Company Six [2000 - 2015]',
      'Company Seven [1986 - 1998]',
      'Company Eight [2011 - 2018]',
      'Company Nine [1980 - 1989]'
    ]
    */
    
    const agesSquare = ages.map(age => Math.sqrt(age));
    console.log(agesSquare);
    /*
    [
      5.744562646538029, 3.4641016151377544,
       4.47213595499958,                  4,
       2.23606797749979, 7.3484692283495345,
       4.58257569495584,    6.6332495807108,
      7.810249675906654,  3.605551275463989,
      3.872983346207417,  6.708203932499369,
                      5,                  8,
      5.656854249492381
    ]
    */
    const agesTimesTwo = ages.map(age => age * 2);
    console.log(agesTimesTwo);
    /*
    [ 66,  24,  40, 32, 10, 108, 42, 88, 122, 26, 30, 90, 50, 128, 64]
    */
    
    const ageMap = ages
        .map(age => Math.sqrt(age))
        .map(age => age * 2);
    console.log(ageMap);
    /*
    [
      11.489125293076057,  6.928203230275509,
        8.94427190999916,                  8,
        4.47213595499958, 14.696938456699069,
        9.16515138991168,   13.2664991614216,
      15.620499351813308,  7.211102550927978,
       7.745966692414834, 13.416407864998739,
                      10,                 16,
      11.313708498984761
    ]
    */

     

     

    4. sort

    // Sort companies by start year
    const sortedCompanies = companies.sort(function(c1, c2){
        if(c1.start > c2.start){
            return 1;
        } else {
            return -1;
        }
    });
    console.log(sortedCompanies);
    
    /*
    [
      { name: 'Company Nine', category: 'Retail', start: 1980, end: 1989 },
      { name: 'Company One', category: 'Finance', start: 1981, end: 2003 },
      { name: 'Company Four', category: 'Retail', start: 1985, end: 2010 },
      { name: 'Company Seven', category: 'Auto', start: 1986, end: 1998 },
      { name: 'Company Two', category: 'Retail', start: 1992, end: 2008 },
      { name: 'Company Three', category: 'Auto', start: 1999, end: 2007 },
      { name: 'Company Six', category: 'Finance', start: 2000, end: 2015 },
      { name: 'Company Five', category: 'Technology', start: 2009, end: 2017},
      { name: 'Company Eight', category: 'Technology', start: 2011, end: 2018}
    ]
    */
    
    // more clean code
    const sortedCompanies2 = companies.sort((a,b) => (a.start > b.start ? 1 : -1));
    console.log(sortedCompanies2);
    
    
    // Sort ages
    const sortAges = ages.sort((a, b) => a - b);
    console.log(sortAges);
    /*
     [ 5, 12, 13, 15, 16, 20, 21, 25, 32, 33, 44, 45, 54, 61, 64 ]
    */
    
    const sortAges2 = ages.sort((a, b) => b - a);
    console.log(sortAges2);
    /*
    [ 64, 61, 54, 45, 44, 33, 32, 25, 21, 20, 16, 15, 13, 12, 5]
    */
    

     

     

    5. reduce

    let ageSum = 0;
    for(let i = 0; i < ages.length; i++){
        ageSum += ages[i];
    }
    console.log(ageSum);
    // 460
    
    const ageSum2 = ages.reduce(function(total, age) {
        return total + age;
    }, 0);
    console.log(ageSum2);
    // 460
    
    // more clean code
    const ageSum3 = ages.reduce((total, age) => total + age, 0);
    console.log(ageSum3);
    
    
    // Get total years for all companies
    const totalYears = companies.reduce(function(total, company){
        return total + (company.end - company.start);
    }, 0);
    console.log(totalYears);
    // 122
    
    // more clean code
    const totalYears2 = companies.reduce((total, company) => total + (company.end - company.start), 0);
    console.log(totalYears2);
    
    
    
    // Combine Methods
    const combined = ages
        .map(age => age * 2)
        .filter(age => age >= 40)
        .sort((a, b) => a - b)
        .reduce((a, b) => a + b, 0);
    
    console.log(combined);
    // 798

     

    'JavaScript' 카테고리의 다른 글

    JavaScript 개요  (0) 2021.06.19
    reduce()  (0) 2021.06.11
    map()  (0) 2021.06.10
    forEach()  (0) 2021.06.10
    3. async & await  (0) 2021.06.10

    댓글

Designed by Tistory.