Array.prototype.filter()

In this example below you will see how to do a Array.prototype.filter() with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by napotopia, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright napotopia ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Array.prototype.filter()</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/napotopia/arrayprototypefilter-QmrbYX */
var animals = [
    { name: 'Tibbi' , species: 'rabbit' },
    { name: 'Benji' , species: 'dog' },
    { name: 'Oreo' , species: 'dog' },
    { name: 'Nemo' , species: 'fish' },
    { name: 'Londen' , species: 'cat' },
    { name: 'Bubbles' , species: 'fish' }
];

// var isDog = function(animal) {
//     return animal.species === "dog";
// };

const filterAnimals = (animal) => {
    return animals.filter((el) => { return el.species === animal; }
  );
}

//var dogs = animals.filter( function(animal) { return animal.species === "dog"});
//var dogs = animals.filter( (animal) => { return animal.species === "dog"});
var cats = filterAnimals("cat");
var dogs = filterAnimals("dog");

console.log(cats);

Comments