Projects > Array JS

Home Projects Challenges

Array JS


June 04, 2021 | Learning JS

Filter the list with condition

        
        
    const inventors = [
      { first: 'Albert', last: 'Einstein', year: 1879, passed: 1955 },
      { first: 'Isaac', last: 'Newton', year: 1643, passed: 1727 },
      { first: 'Galileo', last: 'Galilei', year: 1564, passed: 1642 },
      { first: 'Marie', last: 'Curie', year: 1867, passed: 1934 },
      { first: 'Johannes', last: 'Kepler', year: 1571, passed: 1630 },
      { first: 'Nicolaus', last: 'Copernicus', year: 1473, passed: 1543 },
      { first: 'Max', last: 'Planck', year: 1858, passed: 1947 },
      { first: 'Katherine', last: 'Blodgett', year: 1898, passed: 1979 },
      { first: 'Ada', last: 'Lovelace', year: 1815, passed: 1852 },
      { first: 'Sarah E.', last: 'Goode', year: 1855, passed: 1905 },
      { first: 'Lise', last: 'Meitner', year: 1878, passed: 1968 },
      { first: 'Hanna', last: 'Hammarström', year: 1829, passed: 1909 }
    ];

       
        

Array Prototype Filter

       
     const filteredInvestors = investors.filter(investor => investor.year >= 1500 && investor.year <= 1600);
       // This will return investor with year that is less than 16 and greater than 15 
      
        

Array Protoype Map

        
        const oneArray = investors.map(investor => `${investor.first} ${investor.last}`);
        // Return full names of array
        
        

Array Prototype Sort

        
          const ordered = investors.sort((firstPerson, secondPerson) => firstPerson.year > secondPerson.year ? 1 : -1);
        // Arranging in order whose year is greater, this starts from least to greatest
        
        

Array Prototype Reduce

Basically, it is like finding the total from a list of objects

        
          const totalYears = investors.reduce((total, investor) => {return total += investor.passed - investor.year}, 0); 
          // 0 here is the default value for total
        
        
      const data = ['car', 'car', 'duck', 'dog'];
      const countWord = data.reduce((obj, item) => {
       if (!obj[item]) obj[item] = 0;
       obj[item]++;
       return obj;
      }, {})
        
        

Use Some function to find is there anyone that is older born after 2010?

students.some(student => student.born > 2010); // return true

Use Every function to find if everyone age is more than 20 years?

students.every(student => (student.dies - student.born) > 20);

Use Find function to find if anyone first name is Halsey?

students.find(student => student.first_name === 'Halsey');
// This return an object that contain Halsey as first name
// Check in console.log when click

Use Find Index function to find which index contains 'Doumic' in their email

students.findIndex(student => student.email.includes("doumic"));
// Return index number if exists

Students:

There are 10 records of the students, please check in the console


↤ Go Back

Updated in June 09, 2021
Gmail | Proton | LinkedIn | Github
Alright reserved 2021