0

I have a logical problem that is making me mad in Angular. I have a nested array of objects, which look likes it :

 data : _lignesVent [{
0: {_Status: false, _lstVentilation: Array(2)}
1: {_Status: false, _lstVentilation: Array(0)}
2: {_Status: false, _lstVentilation: Array(5)} }]

each object represents an employee. within each "_lstVentilation" object, there is another array of object like this :

_lstVentilation: Array(2)
0: {_Quantite: 6, _QuantiteTimeStamp: 21600}
1: {_Quantite: 4, _QuantiteTimeStamp: 14400}

What I am trying to to do, is to sum each quantity (by employee). But so far, i only succeeded in summing every "_Quantite" from all of the objects, like that :

addVentilationQuantity() {

var totalQuantity = 0;
    this.data.lignesVent.forEach(ventil => {
        ventil._lstVentilation.forEach(quantite => {
            for(var i = 0; i < quantite._Quantite.length; ++i){
                totalQuantity += quantite._Quantite[i];
            }
        })
    })
    return totalQuantity;
}

And in the HTML I just call the function. So, As a result, for all my employees, there is always the same number, the sum of all the quantity, and not the sum of their quantity.

Can someone help me with this one? Thanks.

JimmyB
  • 99
  • 1
  • 8
  • 1
    `this.data.lignesVent` <= I don't see `lignesVent` in your data markup. `data` is an array, and a weird one at that. The first element of data would be at `data[0]['0']` – Taplar Jan 20 '21 at 15:18
  • Also, calling _that_ function will not result in providing anything - there is no return statement. – Randy Casburn Jan 20 '21 at 15:19
  • My bad, I didnt wrote it properly here. lignesVent is actually right after data, this is where the employees are. This is not a mistake i made in the code. – JimmyB Jan 20 '21 at 15:20
  • addVentilationQuantity() is not returning anything? – Thibs Jan 20 '21 at 15:21
  • 1
    @JimmyB - then please edit/correct the question. – Randy Casburn Jan 20 '21 at 15:21
  • I guess you want to set totalQuantity back to 0 within the first forEach, since that is per employee, and do something with it after each iteration. – James Jan 20 '21 at 15:21
  • you are also right, i copy pasted too quick, i edit – JimmyB Jan 20 '21 at 15:21
  • I just edited the post – JimmyB Jan 20 '21 at 15:24
  • So with the update of the data model, the access path is still wrong. `data.lignesVent[0]['0']` would be the first element – Taplar Jan 20 '21 at 15:24
  • @James yes, i want to iterate on each employee. So I would like to sum the "_Quantite" of each "_lstVentilation". So they can have their own real time. So far, i only succeded in only adding all of their "_Quantite". So they have all the same integer, which is not what i want – JimmyB Jan 20 '21 at 15:29
  • VTR. I don't think the suggested duplicate had much to do with this one. – Scott Sauyet Jan 20 '21 at 19:00
  • It's not clear what you're trying to add. What is an employee, one of the elements in the `lignesVent ` array? Something else? What should the output look like? – Scott Sauyet Jan 20 '21 at 19:04

0 Answers0