[
{
"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.
I tried but unable to do it. Please help
meSubhoKarma:
Product.Price * Qty
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:
A for loop with a total variable to which you keep adding the results of each multiplication.
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 @ilenia