# How do I calculate this?

``````[
{
"id": 97,
"product": {
"price": 200.0
},
"qty": 1.0
},
{
"id": 98,
"product": {
"price": 200.0
},
"qty": 1.0
},
{
"id": 104,
"product": {
"price": 500.0
},
"qty": 1.0
},
{
"id": 105,
"product": {
"price": 500.0
},
"qty": 2.0
}
]
``````

I need to calculate the total of Product.Price * Qty for each object and then calculate the summation.

Youâ€™re moving in the right direction with this line.
Youâ€™ve got an array of objects.
`[{obj}, {obj2}]`
Youâ€™ll need to know two things:

• How do you go through each element in an array?
• How do you access the value of an object key/property?

Think on those, and you should find the answer youâ€™re looking for. If you need more direction, let us know.

2 Likes
• Find a way to iterate the array
• For each item, multiply the objectâ€™s product price by the objectâ€™s qty
• Now, for each of those multiplications, add them all together.

Thereâ€™s two primary ways you can do this:

1. A for loop with a total variable to which you keep adding the results of each multiplication.
2. Using an array method that does this in an abstract way, itâ€™s called reduce.
1 Like

Iâ€™ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

Please use the â€śpreformatted textâ€ť tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.

2 Likes

Thank you so much @luishendrix92 and @ieahleen