code en action
let personnes = {
groupBy(objectArray, property) {
return objectArray.reduce(function (acc, obj) {
let key = obj[property];
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(obj);
return acc;
}, {})
},
list: [
{
nom: "Dupont",
ville: "evry",
sex: "f"
},
{
nom: "Brusel",
ville: "belfort",
sex: "h"
},
{
nom: "Dupont",
ville: "paris",
sex: "f"
},
{
nom: "Durant",
ville : "paris",
sex : "h"}
],
get latest() {
if (this.list.length == 0) {
return undefined;
}
return this.list[this.list.length - 1];
},
get length() {
return this.list.length;
},
get groupeParVilles() {
return this.groupBy(this.list, "ville");
},
get groupeParNom() {
return this.groupBy(this.list, "nom");
},
get groupeParSex() {
return this.groupBy(this.list, "sex");
},
get villes(){
let groupeVilles = this.groupBy(this.list, "ville");
return Object.keys(groupeVilles);
},
get noms(){
let groupeNoms = this.groupBy(this.list, "nom");
return Object.keys(groupeNoms );
},
get sex(){
let groupeSex = this.groupBy(this.list, "sex");
return Object.keys(groupeSex);
},
}
console.log(personnes.villes);
// console.log(personnes.noms);
// console.log(personnes.sex);
console.log(personnes.groupeParVilles);
//console.log(personnes.latest);
//console.log(personnes.length);