A
A
Anton Neverov2015-07-10 12:02:21
JavaScript
Anton Neverov, 2015-07-10 12:02:21

How to add numbers from "base" JSON?

Hello!
I have a small JSON array like this:

[
    {
        "id": 0,
        "price": "100"
    },
    {
        "id": 1,
        "price": "50.95"
    }
]

And it is constantly replenished.
The task is to sum up all "price" values ​​using JavaScript. I am familiar with Json only from ancient scrolls, so I don’t understand it at all. Searched for various solutions in Google - nothing.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valentin Dubrovsky, 2015-07-10
@TTATPuOT

var test = '[{"id": 0,"price": 100},{"id": 1,"price": 50.95}]',
    sum = 0;
test = JSON.parse(test);

for (var i = 0; i < test.length; i++) {
    sum += test[i].price;
}

alert(sum);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question