Pages

reduce

const posts = [
  {id: "moi", upVotes: 20},
  {id: "toi", upVotes: 89},
  {id: "lui", upVotes: 1},
  {id: "eux", upVotes: 123},];


let [premier,second] = posts.reduce((acc, cur) => [cur, ...acc].sort((a,b) => b.upVotes - a.upVotes).slice(0,2),[]);

console.log(`Au second tour sont présents : ${premier.id} et ${second.id}`);

Générateur

class Matrix {
  constructor(width, height, element = (x, y) => undefined) {
    this.width = width;
    this.height = height;
    this.content = [];

    for (let y = 0; y < height; y++) {
      for (let x = 0; x < width; x++) {
        this.content[y * width + x] = element(x, y);
      }
    }
  }

  get(x, y) {
    return this.content[y * this.width + x];
  }
  set(x, y, value) {
    this.content[y * this.width + x] = value;
  }

 *[Symbol.iterator]() {
          let i=0;
   
          while (i < this.height*this.width) {
              yield {
                x : i%this.width,
                y : Math.floor(i/this.width),
                value : this.content[i]
              }
              i++;
          }
  }
 
}


const width = 3;
let matrix = new Matrix(width, width, (x, y) => `[${x},${y}]= ${y * width + x}`);
for (let {value,x,y} of matrix) {
  console.log(x,y,value);
}

https://es6console.com/jppky2fw/

0 0 [0,0]= 0
1 0 [1,0]= 1
2 0 [2,0]= 2
0 1 [0,1]= 3
1 1 [1,1]= 4
2 1 [2,1]= 5
0 2 [0,2]= 6
1 2 [1,2]= 7
2 2 [2,2]= 8 


Autre exemple : lien

Destructuring !

const user = {nom: 'me', age: 54, code: 'secret'};

const old = (function ( para ) {
    return function( {nom} ) {
  return {nom}
}})();
console.log(old(user))


const userSecure = ( ({nom, age}) => ({nom, age}) )(user);

Date

QCM maison

Ecrire sa bibliothèque de fonctions sur les tableaux !

Testez vous

const pers = [
    { nom: "Dupont", ville: "evry", sex: "f", born: 1903 },
    { nom: "Dupont", ville: "Paris", sex: "f", born: 2004 },
    { nom: "Brusel", ville: "belfort", sex: "h", born: 1930 }
];

const filles_callBack = pers.filter(function(p){
    return (p.sex === "f");
});

Comparer ces différentes écritures : 

const filles_callBackArrow = pers.filter(p => p.sex === "f");

const filles_callBackDest = pers.filter(function( {sex}){
    return (sex === "f");
});

const filles_callBackArrowDestruc = pers.filter( ( {sex} ) => sex === "f" );

Pour chaque transformation donner le résultat sous forme graphique :

exemple

const filles= pers.filter(function(p){
    return (p.sex === "f");
});

const dates = filles.map(function annee( p, index ) {
   return { 
        num : index,
        annee : p.born 
     };
});



en Action

    function ageObj({ born }) {return { born }}
    function ageVal({ born }) {return born}
    function ajout(p) {p["new"] = "hallo";}
    function ajout_2(p) {return p["new"] = "hallo";}
    function ageObjDestruct({ born :annee }) {return { annee }};

filles.map(function annee( p, index ) {
   return [p.nom,p.born]
});

filles.map(ajout);

filles.map(ajout_2);

filles.map(p=>p);

filles.map(p=>p["new"]="hallo");

filles.map(ageObj);

filles.map(ageVal);

filles.map(function({ born : annee }) {return { annee }});

filles.map(function({ born :annee, nom }) {return { nom,annee }});

filles.map(function ({ nom, ville }) {
    return {
        info: `${nom} habite ${ville}`
    }
});

https://goo.gl/G6u1Cz