Calculate sum of element array ionic 2

I am developing a mobile application, and I need to calculate for each horse the number of Course in the (rang == 1)

{
Cheval: {
id: "417",
nom: "ALWAN",
sexe: "2",
ne: "2007",
nom_p: "HAJJAM",
ne_m: "1990",
},
CourseHasCheval: [
{
id: "1802",
course_id: "559",
cheval_id: "417",
rang: "1",
temps: "6 L",
},
{
id: "1847",
course_id: "576",
cheval_id: "417",
rang: "3",
temps: "1 L 1/2",
},
{
id: "1906",
course_id: "592",
cheval_id: "417",
rang: "3",
temps: "5 L",
},
{
id: "1950",
course_id: "605",
cheval_id: "417",
rang: "3",
temps: "3 L 1/2",
},
{
id: "1969",
course_id: "612",
cheval_id: "417",
rang: "3",
temps: "1 L 1/2",
},
{
id: "1981",
course_id: "615",
cheval_id: "417",
rang: "3",
temps: "3 L",
jockey_id: "17",
},
{
id: "2001",
course_id: "622",
cheval_id: "417",
rang: "1",
temps: "2.30''5/10",",
},
{
id: "2015",
course_id: "626",
cheval_id: "417",
rang: "3",
temps: "2 L",
},
{
id: "2024",
course_id: "630",
cheval_id: "417",
rang: "3",
temps: "8 L",
},
{
id: "2085",
course_id: "646",
cheval_id: "417",
rang: "5",
temps: "C. TETE",
},
{
id: "2099",
course_id: "651",
cheval_id: "417",
rang: "4",
temps: "2 L",
}
]
},

Iterate over the CourseHasCheval, increment a variable if rang == 1, done.

Need somethign like this?

var data = { Cheval: {
id: "417",
nom: "ALWAN",
sexe: "2",
ne: "2007",
nom_p: "HAJJAM",
ne_m: "1990",
},
CourseHasCheval: [
{
id: "1802",
course_id: "559",
cheval_id: "417",
rang: "1",
temps: "6 L",
},
{
id: "1847",
course_id: "576",
cheval_id: "417",
rang: "3",
temps: "1 L 1/2",
},
....
]
};



var horsesSum = [];
for(var i = 0; i < Object.keys(data.CourseHasCheval).length; ++i) {
	var item = data.CourseHasCheval[i];

	if (!horsesSum["Cheval_"+item.cheval_id]) {
		horsesSum["Cheval_"+item.cheval_id] = [];
	}
	if (!horsesSum["Cheval_"+item.cheval_id]["Rang_"+item.rang]) {
		horsesSum["Cheval_"+item.cheval_id]["Rang_"+item.rang] = 0;
	}
	horsesSum["Cheval_"+item.cheval_id]["Rang_"+item.rang]++;
}
console.log(horsesSum);

??

The result will be something like this:

[ Cheval_417: [ Rang_1: 2, Rang_3: 7, Rang_5: 1, Rang_4: 1 ] ]

…2 time first, 7 times third, aso…

No, i need a method that returns the number of race that has rang == 1

How exactly is it done please

You should try filter function for arrays on JS.

It would be something like this:

var racesArray = [];
var filteredArray = racesArray.filter(function(race){
return (race.rang == '3');
})

Or in ES6

let filteredArray = racesArray.filter((race) => race.rang == '3' )

How to make a call to the template ?

You don’t. The template binds to properties in the controller. You change the backing property, the template updates automatically.

Really sorry, I do not understand

I’ve lost track of which of your many different threads on this exact same topic I’ve said what in before.

Once more, with feeling.

You are looping across an array in your template. You talk about horses and races and courses, but I still have no idea what you are trying to loop across. I’m going to pretend it’s horses.

<div *ngFor="let horse of horses">
Name: {{horse.name}}
Home Track: {{horse.homeTrack}}
Number of Races Last Year: {{horse.numberOfRacesLastYear}}
</div>

Write the template out first. Define a property (like numberOfRacesLastYear) for everything you want to display in the template.

Then go to the controller. Whereever you are getting the data from, be it a storage Promise or an Http Observable’s subscription, that is where you need to populate the horses array with a bunch of objects that have all the properties the template wants. That is where you calculate your sums, and you stick the results into the numberOfRacesLastYear property of each horse. Once that is done, the template will display what you have put into each horse object.

But how to create a model like numberOfRacesLastYear ornumberOfRacEachYear ?
This is my problem !!!:cry:

Other people have given you good advice about that. The one thing that I am trying to get across to you is that you need to solve that problem COMPLETELY IN CONTROLLER CODE. The template has absolutely no role in computation.

Yes I know well that the calculation takes place by side controleur but how? The example given by the other does not work !!

I get that you’re frustrated, but “does not work” conveys no useful information. What would is:

Here is the code I tried:

code goes here

Here is what I fed it:

complete JSON goes here

Here is what I was expecting:

horses: [ 
  { "name": "Secretariat", "racesLastYear": "6" },
  { "name": "Affirmed", "racesLastYear": "2"}
]

Here is what I got:

horses: [ 
  { "name": "Secretariat", "racesLastYear": "0" },
  { "name": "Affirmed", "racesLastYear": "banana"}
]

Please give me how to create a model or method to calculate the racing nomber with (rang== 1)

No, I’m done playing this game by your rules. You want further help from me, we’re going to play by my rules. Fill out my template above.

nbr: number;
// other variables

getNb() {
  // ...
  this.members = data.CourseHsCheval ;
if ( curent.CourseHsCheval.rang ==1) {
  this.nbr = this.members.reduce((previous, current) => {
    return previous + 1 ; }
  }, 0);
}
{
Cheval: {
id: "417",
nom: "ALWAN",
sexe: "2",
ne: "2007",
nom_p: "HAJJAM",
ne_m: "1990",
},
CourseHasCheval: [
{
id: "1802",
course_id: "559",
cheval_id: "417",
rang: "1",
temps: "6 L",
},
{
id: "1847",
course_id: "576",
cheval_id: "417",
rang: "3",
temps: "1 L 1/2",
},
{
id: "1906",
course_id: "592",
cheval_id: "417",
rang: "3",
temps: "5 L",
},
{
id: "1950",
course_id: "605",
cheval_id: "417",
rang: "3",
temps: "3 L 1/2",
},
{
id: "1969",
course_id: "612",
cheval_id: "417",
rang: "3",
temps: "1 L 1/2",
},
{
id: "1981",
course_id: "615",
cheval_id: "417",
rang: "3",
temps: "3 L",
jockey_id: "17",
},
{
id: "2001",
course_id: "622",
cheval_id: "417",
rang: "1",
temps: "2.30''5/10",",
},
{
id: "2015",
course_id: "626",
cheval_id: "417",
rang: "3",
temps: "2 L",
},
{
id: "2024",
course_id: "630",
cheval_id: "417",
rang: "3",
temps: "8 L",
},
{
id: "2085",
course_id: "646",
cheval_id: "417",
rang: "5",
temps: "C. TETE",
},
{
id: "2099",
course_id: "651",
cheval_id: "417",
rang: "4",
temps: "2 L",
}
]
},

That is not relevant code. That is a template. I want to see how you are attempting to build the sum in controller code. You also did not describe expected and actual behavior.

i’m editing comment !!!