Usage of For loops

Hi,
In the following code, I would like to get the sum(item.qty) only for a particular item_code

total_qty = sum((item.qty for item in self.get(“items”)))

How do I do that?

thanks
Krithi

@krithi_ramani where? Client-side or Backend? JavaScript or Python sample?

total_qty = sum([item.qty for item in self.get("items") if item.item_code = 'x'])
3 Likes

Thanks @rmehta!

Shouldn’t it will run in backend? Just in curiosity, how can i do this in client-side? I am looking for just a simple demonstration, nothing else.

@ni8mr

var sum = 0;
cur_frm.doc.items.forEach(function(item){
    sum += item.item_code === 'x' ? item.qty : 0;
});
2 Likes

Thank you :slight_smile:

how to use for loop in items
like
for (var i;i<=doc.items.length ;i++)

for (var i =0; i < cur_frm.doc.items.length; i++)
 {
	cur_frm.doc.items[i].date = cur_frm.doc.delivery_date;
 }
cur_frm.refresh_field('items');

Hi @NCP , does this one can be apply/use in custom print format which use Jinja templating ? Can you guide/ show an example please ?

Thankyou in advanced.