Pages

en action

pers = [
{
nom: "Dupont",
ville: "evry",
sex: "f"
},
{
nom: "Brusel",
ville: "belfort",
sex: "h"
},
{
nom: "Dupont",
ville: "paris",
sex: "f"
},
{
nom: "Durant",
ville : "paris",
sex : "h"}
];

//for of

function Affiche(tab, callback) {
let i=0;
for (let ele of tab){
console.log(callback ? callback(ele,i) : `${i}->${ele.nom}`);
i++;
}
};

function afficheVille({ville},i) {
return `${i} : ${ville}`
};

Affiche(pers,afficheVille);


// transforme
function civilite({nom,sex}) {
return (sex=='h' ? `Monsieur ${nom}` : `Madame ${nom}`);
}

function politesse({nom,sex}) {
return (sex=='h' ? `Cher ${nom}` : `Chere ${nom}`);
}
function transf(array, fx) {
let passed = [];
for(let v of array)
passed.push(fx(v));
return passed;
}

console.log(transf(pers,civilite));
console.log(transf(pers,politesse));


//map

function toStringMap(tab,callback) {
let t = tab.map((ele,i) => callback ? callback(ele,i) : `${i} -> ${ele.nom}`);
//console.log(t);
return t.toString();
}
//console.log(toStringMap(pers));
//console.log(toStringMap(pers,afficheVille));