Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

php - Adding up a value from a nested object

I am working on a project to get orders from an API and display them in a dashboard with some basic information (my first project). Currently what i am displaying looks like this:enter image description here

Fields from left to right: Date, Amount of products in order, price

I'm currently looping through all products in an order and display the amount, therefore it displays "1 15", the same goes for price. I want to add them up instead and just display "16".

I'm trying to create a method to do so but i cant seem to get it working. This is my current code.

HTML

<tr v-for="(order, index) in orders"
                            v-bind:class="index % 2 === 0 ? 'bg-white' : 'bg-gray-50'">
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                {{ order.id }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                {{ order.deliveryName }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                {{ order.createdAt }}
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                <span v-for="(product) in order.products">
                                    {{ product.amount }}
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                <span v-for="(product) in order.products">
                                    {{ product.amount * product.price }}<br>
                                </span>
                            </td>

i tried removing the order.products loop and instead do so in a method but i couldn't get it working. For example (i tried tweaking the method around a bit to no avail)

In HTML: {{ getOrderAmount(order)}}

In SCRIPT:

getOrderAmount: function (order) {
        let amount = 0;
        order.forEach(product) in order.products
        {
            amount += product.amount
        }
        return amount;
    },

How can i go about this. Help is much appreciated!


question from:https://stackoverflow.com/questions/65901745/adding-up-a-value-from-a-nested-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could map your orders by summing the product amount/prive using a computed property then use that property inside the template :

computed(){
  mappedOrders(){
        this.order.map((order)=>{
                 order.productsAmount=order.products.reduce((acc,curr)=>{
                     return acc+=curr.amount;
                },0)
        
     order.sumPrices=order.products.reduce((acc,curr)=>{
                     return acc+=curr.amount * curr.price ;
                },0)

        return order;
      })

   }
}

in template :

<tr v-for="(order, index) in mappedOrders" ..>
     ...
  <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                <span >
                                    {{ order.productsAmount}}
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                <span >
                                    {{ order.sumPrices}}<br>
                                </span>
                            </td>
</tr>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...