0

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:

test

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

6
  • 1
    What error do you get? Commented Jan 4, 2018 at 14:24
  • I get undefined Commented Jan 4, 2018 at 14:25
  • you never initialized total Commented Jan 4, 2018 at 14:27
  • undefined is not an error. Commented Jan 4, 2018 at 14:27
  • 1
    also, total is part of series, not of data. So it would be data[0]["total"] at best Commented Jan 4, 2018 at 14:29

1 Answer 1

1

You can try something like this;

let total = 0;
data.forEach(function (item) {
    if(item.hasOwnProperty('total')) {
        total = item.total 
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Probably a problem with scope. If I don't see your code I cannot help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.