cristinafsanz / code-snippets

Example code with gifs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

code-snippets

Code snippets with gifs, to avoid copy and paste the code and write it all.

Table of Contents

forEach

Example:

const array = ['this', 'is', 'a', 'test'];

array.forEach((item, index) => {

});

Gif:

Escribir forEach paso a paso

reduce

Example:

var pilots = [
  {
    id: 10,
    name: "Poe Dameron",
    years: 14,
  },
  {
    id: 41,
    name: "Tallissan Lintra",
    years: 16,
  },
];

const totalYears = pilots.reduce((acc, pilot) => acc + pilot.years, 0);

Gif:

Escribir forEach paso a paso

Useful use cases.

filter

Example:

const fifteen = inventors.filter(function(inventor) {
  if(inventor.year >= 1500 && inventor.year < 1600) {
    return true;
  }
});
const fifteen = inventors.filter(inventor => (
   inventor.year >= 1500 && inventor.year < 1600
));

map

Example:

const fullNames = inventors.map(inventor => (
    `${inventor.first} ${inventor.last}`
));

sort

Example:

const ordered = inventors.sort(function(a, b) {
  if(a.year > b.year) {
    return 1;
  } else {
    return -1;
  }
});
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);

Note: Examples from JavaScript 30 from Mike Ekkel

Files for each snippet

There is a file for each functionality, to be able to execute it.

Example:

  • File es6/forEach/forEach.js

    • Make it executable:
    chmod +x es6/forEach/forEach.js
    
    • Run it:
    ./es6/forEach/forEach.js
    

About

Example code with gifs


Languages

Language:JavaScript 100.0%