Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sum only those objects of the array that require the status (true)

I have an array with such data (more data in reality)

         [ 
           {
                 serviceid: "979cf8e6",
                 amount: 1,
                 price: 11,
                 materialsPrice: 100,
                 enable: true,
                 chenge: true
            },
            {
                 serviceid: "979cf812",
                 amount: 1,
                 price: 15.5,
                 materialsPrice: 0,
                 enable: true,
                 chenge: true
            }
         ]

I want to match all "price" in an array in which change = true. Now I’m using this query for this.

   double get sumPay {
    double sum = listVariant
        .map((e) => e.price )
        .fold(0, (previousValue, price) => previousValue + price!);
    return sum;
  }

But this request sums up all the elements, and it will give me only those in which the status is change: true. I will be grateful for your help)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this

   double get sumPay {
    var changeList = listVariant.where((e) => e.change == true);
    double sum = changeList
        .fold(0, (previous, next) => previous.price + next.price);
    return sum;
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading