Calculate sum of element array ionic 2


#1

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",
}
]
},

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

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


#3

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);

??


#4

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…


#5

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


#6

How exactly is it done please


#7

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' )

#8

How to make a call to the template ?


#9

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


#10

Really sorry, I do not understand


#11

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.


#12

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


#13

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.


#14

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


#15

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"}
]

#16

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


#17

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.


#18
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",
}
]
},

#19

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.


#20

i’m editing comment !!!