class : Action
class Prefix {
constructor(pref) { // pas de fx arrow
this.pref= pref;
}
addPref(tab) {
return tab.map( ( {genre,nom} ) => {
let pref = genre =='m' ? `${this.pref}` : `${this.pref}e`;
return `${pref} ${this.upper(nom)}`
})
}
upper(s){
return s[0].toUpperCase() + s.slice(1);
}
}
pers = [
{nom: "Dupont",ville: "evry",genre: "f"},
{nom: "Brusel",ville: "belfort",genre: "m"}
];
let t = new Prefix("Cher");
console.log(t.addPref(pers));
Mélanger un tableau !
On veut mélanger les éléments d'un tableau :
["denis","gérard","nicolas","lolo"]
["nicolas","gérard","lolo","denis"]
idem en 1
["denis","gérard","nicolas","lolo"]
["denis","gérard","nicolas","lolo"]
idem en 0
["denis","gérard","nicolas","lolo"]
["nicolas","lolo","denis","gérard"]
TOP
Améliorer ce code :
test map !
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");
});
const filles_callBackDest = pers.filter(function( {sex}){
return (sex === "f");
});
const filles_callBackArrowDestruc = pers.filter( ({sex} ) => sex == "f" );
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
{ 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 }};
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 }};
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
Map en Action
Comment transformer :
[['eux', ' ', ' ' ],['A', 'B', 'C' ],['eux', ' ', ' ']]
en
[["eux", "A", "eux"],[" ", "B", " "],[" ", "C", " "]]
Aide
Inscription à :
Articles (Atom)