1

i use this code to display all persons i have.

<div class="col-md-3" v-for="person in persons">
</div>   

//Each Person is an Object. in Each Object there are multiple objects(Money Given). i Need to Calculate the sum of total amount (Array) existing in those objects (All money given to each person)

//Like to say, all the credits relative to each Person.

I use to do that with Php, but how can i do that with computed ?

Vuejs code :

var app = new Vue({
el: '#app',
data: {
   persons:[],
},
methods: {
getPersons (),
},

Thanks you !

Here is a sample of persons. multiples objects, and inside each object, usercashfloats with multiple objects i need to calculate the amount . (Amount exist in each these objects) enter image description here

5
  • please provide a sample of persons data Commented Dec 27, 2018 at 19:44
  • Thank you sir, i add a picture for u, i want to calculate the sum of amount existing in all objects existing in usercashfloat Commented Dec 27, 2018 at 19:51
  • my bro could you expand one item in usercashfloat? Commented Dec 27, 2018 at 19:54
  • i provided an answer by assuming that each item in usercashfloat array has amount property Commented Dec 27, 2018 at 20:01
  • Yes, sir, in each object inside usercashfloat there is amount.. Commented Dec 27, 2018 at 21:11

2 Answers 2

2

Your computed property should be like :

  computed:{
      personsWithAount(){

             const reducer = (accumulator, currentValue) => accumulator.amount + currentValue.amount;
              return  this.persons.map((pers)=>{
                      let p=pers;
                      if(pers.usercashfloat.length==0){

                       p.totalAmount=0;
                       }else if(pers.usercashfloat.length==1){
                           p.totalAmount=pers.usercashfloat[0].amount;

                       }else{
                            p.totalAmount=pers.usercashfloat.reduce(reducer);
                       }
                     return p;
                     });


          }
       }

reduce function doc

and in your template you will have something like :

 <div class="col-md-3" v-for="person in personsWithAmount">
     {{person.totalAmount}}
 </div>   
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir, i got error during evaluation .. also, how i'm supposed to call total of each person in blade ?
2

It could be like this:

<div class="col-md-3" v-for="person in preparedPersons">
</div> 

var app = new Vue({
el: '#app',
data: {
   persons:[],
},
computed: {
   preparedPersons : function() {
      return this.persons.map(
         (p) => {
             return {
                   ...p, 
                   sumOfAmount: p.usercashfloat.reduce(
                       (acc, cur) => acc.amount + cur.amount
                   )
              };
          }
      )
   }
},
methods: {
getPersons (),
},

5 Comments

Thank you sir, i add a picture for u, i want to calculate the sum of amount existing in all objects existing in usercashfloat's objects
what is structure of usercashfloat object ?
@yassinej I made changes to the answer so it should works for you fine
Thank you sir, i got error during evaluation .. also, how i'm supposed to call total of each person in blade ?
it will be like <span>Amount is {{person.sumOfAmount}} </span>

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.