group object
by Sulthan Allaudeen
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
<div id="content">
</div>
JavaScript
var list = [{
"_id": "634951b1f5d408cff4572635",
"name": "Raj",
"date": "2022-10-11",
"bill_no": 0,
"total_amount": 200,
"bank": 1,
"terminal": 0,
"credit": 0,
"cash": 0,
"sell_due": 87886.98000000001,
"added_by": "John"
},
{
"_id": "634951bcf5d408cff457263f",
"name": "Raj",
"date": "2022-10-11",
"bill_no": 0,
"total_amount": 200,
"bank": 2,
"terminal": 0,
"credit": 0,
"cash": 0,
"sell_due": 35,
"added_by": "John"
},
{
"_id": "63495261f5d408cff4572652",
"name": "Ramana",
"date": "2022-10-11",
"bill_no": 0,
"total_amount": 300,
"bank": 0,
"terminal": 0,
"credit": 0,
"cash": 0,
"sell_due": 35,
"added_by": "John"
}
]
var groups = _.groupBy(list, function(value) {
return value.user_id + '#' + value.alert_id;
});
var groups = _.groupBy(list, function(value) {
return value.name + '#' + value.date;
});
// console.log(groups)
var data = _.map(groups, function(group) {
return {
name: group[0].name,
date: group[0].date,
bank: group[0].bank,
terminal: group[0].terminal,
credit: group[0].credit,
cash: group[0].cash,
sell_due: group[0].sell_due,
added_by: group[0].added_by,
ids: _.pluck(group, '_id')
}
});
var output = [];
for (let index = 0; index < data.length; index++) {
const element = data[index];
console.log(element);
console.log(element.ids)
var total_amount = 0;
for (let indexx = 0; indexx < element.ids.length; indexx++) {
var elementt = element.ids[indexx];
var foundList = _.findWhere(list, {
_id: elementt
});
console.log('found list,', foundList)
total_amount = parseInt(total_amount) + parseInt(foundList.total_amount);
}
var obj = {
"name": element.name,
"date": element.date,
"ids": element.ids,
bank: element.bank,
terminal: element.terminal,
credit: element.credit,
cash: element.cash,
sell_due:...