Calculating sum of elements of array in Angular 2 and ionic 2 ngfor


#1

I develop a mobile application with ionic 2 and I need for each horse to calculate the allocation sum (Couse.allocation) for each year,
Par example :
Table: Course (Race )
[Id_course: 1, allocation: 200, date: 10/03/2012, idcheval: 1]
[Id_course: 2, allocation: 300, date: 11/07/2012, idcheval: 1]
[Id_course: 3, allocation: 700, date: 10/03/2013, idcheval: 1]
[Id_course: 4, allocation: 600, date: 10/03/2013, idcheval: 1]
Engagemen table:
[2012, 2013]
So display like that:

2012 ====> 500
2013 ====> 1300

Can you help me please?

Json file

{
course_cheval: [
{
CourseHasCheval: {
id: "1294",
course_id: "408",
cheval_id: "182",
rang: "2",
temps: "5 L",
jockey_id: "23",
poids: "54",
part_jokey: "147.000",
entraineur_id: "6",
part_entraineur: "165.000",
propritaire: "TORGEMAN. F",
part_propritaire: "1650.000",
eleveur_id: "20",
part_eleveur: "825.000"
},
Course: {
id: "408",
date: "2013-12-22",
nom_du_prix: "ROUBAYIAT",
allocation: "7500",
hippodrome_id: "2",
jouree: "72",
categorie_id: "3",
distance: "1600",
nbre_partant: "8",
detail: " 5ème Course PRIX ROUBAYIAT (GR.3) Le 22 Déc 2013. Pouliches de 3 ans Pur Sang Arabe. 7.500 Dinars (4.125 - 1650 - 825 - 525 - 375) Primes (2.062.5 - 825 - 412.5 - 262,5) Entrée : 41 d,250 - Forfaits : 13 d,750 & 27 d,500 Pour pouliches de 3 ans de Pur Sang Arabe Poids 54 kg - Surcharge : 1 kg par course de G3 - 3 kg par course de G2, 5 kg par course de G1 - Surcharges cumulables. Distance : 1600 mètres environ. ",
video: "",
international: false,
etranger: "0"
},
Cheval: {
id: "182",
nom: "DAYJURA",
sexe: "2",
robe_id: "4",
ne: "2010",
race_id: "1",
code: "7957",
nom_p: "KERBELLA",
ne_p: "1992",
nom_m: "ISTIHBAB",
ne_m: "1996",
nom_pm: "CHEIKH EL ARAB",
ne_pm: "1990",
etat: "1",
eleveur_id: "20",
propritaire: "TORGEMAN. F",
photo: "",
export: "2000",
pays: "",
mise_service: null,
production: "0000"
},
},
{
CourseHasCheval: {
id: "583",
course_id: "187",
cheval_id: "182",
rang: "5",
temps: "5 L",
jockey_id: "23",
poids: "54",
part_jokey: "45.000",
entraineur_id: "6",
part_entraineur: "38.000",
propritaire: "TORGEMAN. F",
part_propritaire: "375.000",
eleveur_id: "20",
part_eleveur: "0.000"
},
Course: {
id: "187",
date: "2014-02-02",
nom_du_prix: "IKMEL",
allocation: "7500",
hippodrome_id: "2",
jouree: "6",
categorie_id: "3",
distance: "1600",
nbre_partant: "7",
detail: " 5ème Course PRIX IKMEL (GR.3) Le 02 Février 2014. Pouliches de 4 ans Pur Sang Arabe. 7.500 Dinars (4.125 - 1650 - 825 - 525 - 375) Primes (2.062.5 - 825 - 412.5 - 262,5) Entrée : 41 d,250 - Forfaits : 13 d,750 & 27 d,500 Pour pouliches de 4 ans de Pur Sang Arabe - Poids 54 kg - Surcharge : 1 kg par course de G3, 3 kg par course de G2 - 5 kg par course de G1 - Surcharges cumulables. Distance : 1600 mètres environ. ",
video: "",
international: false,
etranger: "0"
},
Cheval: {
id: "182",
nom: "DAYJURA",
sexe: "2",
robe_id: "4",
ne: "2010",
race_id: "1",
code: "7957",
nom_p: "KERBELLA",
ne_p: "1992",
nom_m: "ISTIHBAB",
ne_m: "1996",
nom_pm: "CHEIKH EL ARAB",
ne_pm: "1990",
etat: "1",
eleveur_id: "20",
propritaire: "TORGEMAN. F",
photo: "",
export: "2000",
pays: "",
mise_service: null,
production: "0000"
},
},
{
CourseHasCheval: {
id: "661",
course_id: "210",
cheval_id: "182",
rang: "1",
temps: "1.39''9/10",
jockey_id: "27",
poids: "55",
part_jokey: "330.000",
entraineur_id: "6",
part_entraineur: "330.000",
propritaire: "TORGEMAN. F",
part_propritaire: "3300.000",
eleveur_id: "20",
part_eleveur: "1650.000"
},
Course: {
id: "210",
date: "2014-03-09",
nom_du_prix: "OUID’AA",
allocation: "6000",
hippodrome_id: "2",
jouree: "15",
categorie_id: "4",
distance: "1400",
nbre_partant: "7",
detail: " 1ère Course PRIX OUID’AA (L.R) Le 09 Mars 2014. Pouliches & juments de 4 ans et plus Pur Sang Arabe. 6.000 Dinars (3.300 - 1.320 - 660 - 420 - 300) Primes (1.650 - 660 - 330 - 210) Entrée : 33 d,00 - Forfaits : 11 d,00 & 22 d,00 Pour pouliches et juments de 4 ans et plus de Pur Sang Arabe n’ayant pas gagné une course de G1 ou de G2 depuis le 01/01/2014 ni 10.500 DT depuis le 01/05/2014 - Poids 54 kg - Surcharge 1 kg par 2.000 DT, Décharge 2 kg pour celles n’ayant jamais gagné une course de groupe. Distance : 1400 mètres environ.",
video: "",
international: false,
etranger: "0"
},
Cheval: {
id: "182",
nom: "DAYJURA",
sexe: "2",
robe_id: "4",
ne: "2010",
race_id: "1",
code: "7957",
nom_p: "KERBELLA",
ne_p: "1992",
nom_m: "ISTIHBAB",
ne_m: "1996",
nom_pm: "CHEIKH EL ARAB",
ne_pm: "1990",
etat: "1",
eleveur_id: "20",
propritaire: "TORGEMAN. F",
photo: "",
export: "2000",
pays: "",
mise_service: null,
production: "0000"
},

},
{
CourseHasCheval: {
id: "687",
course_id: "219",
cheval_id: "182",
rang: "4",
temps: "3 L 1/2",
jockey_id: "27",
poids: "54",
part_jokey: "57.000",
entraineur_id: "6",
part_entraineur: "53.000",
propritaire: "TORGEMAN. F",
part_propritaire: "525.000",
eleveur_id: "20",
part_eleveur: "263.000"
},
Course: {
id: "219",
date: "2014-03-30",
nom_du_prix: "MEZIANA",
allocation: "7500",
hippodrome_id: "2",
jouree: "19",
categorie_id: "3",
distance: "1600",
nbre_partant: "5",
detail: " 2ème Course PRIX MEZIANA (GR.3) Le 30 Mars 2014. Pouliches de 4 ans Pur Sang Arabe. 7.500 Dinars (4.125 - 1650 - 825 - 525 - 375) Primes (2.062.5 - 825 - 412.5 - 262,5) Entrée : 41d, 250 – Forfaits : 13d, 750 & 27d, 500 Pour pouliches de 4 ans de Pur Sang Arabe - Poids 54 kg - Surcharge : 1 kg par course de G3, 3 kg par course de G2 - 5 kg par course de G1 - Surcharges cumulables. Distance : 1600 mètres environ.",
video: "",
international: false,
etranger: "0"
},
Cheval: {
id: "182",
nom: "DAYJURA",
sexe: "2",
robe_id: "4",
ne: "2010",
race_id: "1",
code: "7957",
nom_p: "KERBELLA",
ne_p: "1992",
nom_m: "ISTIHBAB",
ne_m: "1996",
nom_pm: "CHEIKH EL ARAB",
ne_pm: "1990",
etat: "1",
eleveur_id: "20",
propritaire: "TORGEMAN. F",
photo: "",
export: "2000",
pays: "",
mise_service: null,
production: "0000"
},

,

},
],
engagements: [
[
{
annee: "2014"
}
],
[
{
annee: "2013"
}
]
]
}

Template

<ion-row *ngFor="let ch of cheval1 ">
      {{ch[0].annee }}
    <div *ngFor="let m of members  ; let rowIndex = index">
            <ion-col *ngIf="ch[0].annee == (m.Course.date |date : 'yyyy' )">
            {{ m.Course.allocation}} 
            </ion-col>
     </div>

Component

allocationSum: number;
// other variables

getIdCheval() {
  
  this.allocationSum = this.members.reduce((previous, current) => {
    return previous + parseInt(current.Course.allocation);
  }, 0);
}

#2

Didn’t you like the answers on Calculate sum of element array ionic 2 or Pipe ionic return object?


#3

No answer correct, all answer does not work


#4

In this case, instead of creating multiple new posts, follow up on the original one with feed back.
Please do not spam the forum with the same question over and over.


#5