World.prototype.toString = function() {
  return (
    this.grid.getSpace().map( (elt) => charFromNb(legendInv, elt))
                        .reduce((p,n,i) => (i%this.grid.width===0) ? (p+"\n"+n) : (p+n))
  );
};

test js

reduce

'use strict';

let t = ["abcdefghi"];

let MyA = t[0].split("");

/*
let v = MyA.reduce(function(p,n,i,array){
  //console.log(p,n,i,i%3);
  return (i%3===0) ? (p+"\n"+n) : (p+n);
});
*/

let v = MyA.reduce((p,n,i) => (i%3===0) ? (p+"\n"+n) : (p+n));
console.log(v);
World.prototype.set = function(){
    map.forEach( (line,y) => {
      line.split("").forEach( (char,x) => {
           this.grid.set(new Vector(x,y),nbFromChar(legend,char));
      });
    });
};

Array.from

Array.from permet d'initialiser un tableau.

 const T = Array.from(new Array(5), (x,i) => i);
console.log(T);

// il suffit d'avoir un itérateur :
 const tab = Array.from({length : 5}, (x,i) => 2*i);
console.log(tab);

code


JS Bin on jsbin.com
Référenc
 Array.from
Voici un cas d'écriture de fonction fléchées :

var motL = "anticonstitutionnelement myélosaccoradiculographie cyclopentanoperhydrophénanthrène intergouvernementalisation";

motT = motL.split(" ");

console.log(motT.map(el => el.length).reduce((a, b) => Math.max(a, b)));

console.log(motT.reduce((a, b) => a.length > b.length ? a : b));


Voici une utilisation sur le DOM ! 

motL = document.querySelector("div");

motT = motL.innerHTML.split(" ");

var Max = motT.reduce((a, b) => a.length > b.length ? a : b);


motL.innerHTML = motL.innerHTML.replace(Max, "<span>"+Max+"</span>");



Lire l'article suivant pour découvrir le this lexical dans les fonctions fléchées.

Class

class Nb { constructor( x ) { this.x = x; } carre () { return this.x * this.x; } } const p1 = new Nb(2); console.log(p1.carre());