I have a list of array. Avery array has an object, and I want to get the value of this object
My code as follow:
var data =[]
var children= response.data.children
children.forEach(function (child) {
if(equipes.hasOwnProperty(child['id'])) {
total += equipes[child['id']];
dataequipes.push({
id: child['id'],
total: equipes[child['id']]
});
}
});
series.domain = domainId;
series.total = total;
series.details = dataequipes;
data.push(series);
return data;
In my other function I call the returned value and the result as follows:
But when I want to call the total value of object, I got always error:
I used data.total, data['total']... but without any result
Cerbrus
73.3k19 gold badges138 silver badges151 bronze badges
asked Jan 4, 2018 at 14:21
Majdi Taleb
7613 gold badges9 silver badges26 bronze badges
1 Answer 1
You can try something like this;
let total = 0;
data.forEach(function (item) {
if(item.hasOwnProperty('total')) {
total = item.total
}
});
answered Jan 4, 2018 at 15:20
Roumelis George
6,7462 gold badges19 silver badges31 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Majdi Taleb
Another problems: you see that variable total is set to zero: so if a do total=total+item.total , the total is incremented, but when I do console.log(total) outside the boucle I get 0. I'm working with angularjs but really I can't understand why the variable total is zero. normally should be modified to his final value.
Roumelis George
Probably a problem with scope. If I don't see your code I cannot help you.
lang-js
totaltotalis part ofseries, not ofdata. So it would be data[0]["total"] at best