for instance
<div class="price">{{blocks.quantity}} x {{blocks.price}} </div>
i want to multiply price by quantity
data from Json file.
var nunjucks = require('nunjucks');
var env = nunjucks.configure();
env.addFilter('mysum', function (arr) {
return arr
.map(e => e.quantity * e.price) // get amount to each e
.reduce((sum, e) => sum + e, 0) // calc total summa
});
var data = [
{price: 10, quantity: 2},
{price: 2, quantity: 7},
{price: 5, quantity: 11}
]
var res = env.renderString(`{{ data | mysum }}`, {data});
console.log(res);