so i have an response from my mongodb as result of my aggregation function using mongopaginateaggregate as below:
const data = {
docs: [
{
Material_Description: 'NABATI VITAKRIM RSY 122g MT (24 pcs)',
Plant: 'K105',
Channel: 'MT',
Material: '300973',
expiredDate: 'January-2024',
'2023-04-12': 124
},
{
Material_Description: 'NEXTAR GANDUMKU RCO 22g GT(10pc x 12bal)',
Plant: 'K105',
Channel: 'GT',
Material: '303923',
expiredDate: 'October-2023',
'2023-04-12': 1
}
],
totalDocs: 2,
limit: 1000,
page: 1,
totalPages: 1,
pagingCounter: 1,
hasPrevPage: false,
hasNextPage: false,
prevPage: null,
nextPage: null
}
i try to group it and compare each value by adding function datelist:
const dateLists = Array.from({ length: 7 }, (_, index) => {
const local = new Date();
local.setDate(local.getDate() - index);
return local.toLocaleDateString("en-SE", {
timeZone: "Asia/Jakarta",
});
});
here is my try and my result:
i succeed adding other dates to the data but failed to compare each date value to get the comparasion:
const new_data = data.docs.map((item) => {
const newItem = {
Material: item.Material,
Material_Description: item.Material_Description,
Plant: item.Plant,
Channel: item.Channel,
expiredDate: item.expiredDate,
};
let a = 1;
dateLists.sort().forEach((date) => {
newItem[date] = item[date] ? item[date] : 0;
// `${'Keterangan_'+ parseStr(a)}` = item[date] ? item[date] : 0;
});
return newItem;
});
data.docs = new_data;
result:
{
"docs": [
{
"Material": "300973",
"Material_Description": "NABATI VITAKRIM RSY 122g MT (24 pcs)",
"Plant": "K105",
"Channel": "MT",
"expiredDate": "January-2024",
"2023-04-12": 124,
"2023-04-13": 0,
"2023-04-14": 0,
"2023-04-15": 0,
"2023-04-16": 0,
"2023-04-17": 0,
"2023-04-18": 0
},
{
"Material": "303923",
"Material_Description": "NEXTAR GANDUMKU RCO 22g GT(10pc x 12bal)",
"Plant": "K105",
"Channel": "GT",
"expiredDate": "October-2023",
"2023-04-12": 1,
"2023-04-13": 0,
"2023-04-14": 0,
"2023-04-15": 0,
"2023-04-16": 0,
"2023-04-17": 0,
"2023-04-18": 0
}
],
"totalDocs": 2,
"limit": 1000,
"page": 1,
"totalPages": 1,
"pagingCounter": 1,
"hasPrevPage": false,
"hasNextPage": false,
"prevPage": null,
"nextPage": null
}
is that possible to compare each value of the date like this?:
here is my expected resulted that when compare each value of the date inside the object:
"docs": [
{
"Material": "300973",
"Material_Description": "NABATI VITAKRIM RSY 122g MT (24 pcs)",
"Plant": "K105",
"Channel": "MT",
"expiredDate": "January-2024",
"2023-04-12": 124,
"Keterangan_1":"No Data",
"2023-04-13": 0,
"Keterangan_2":"Berkurang",
"2023-04-14": 0,
"Keterangan_3":"Sama",
"2023-04-15": 0,
"Keterangan_4":"Sama",
"2023-04-16": 0,
"Keterangan_5":"Sama",
"2023-04-17": 0,
"Keterangan_6":"Sama",
"2023-04-18": 0,
"Keterangan_7":"Sama",
},
any help on this… or any one can help to improve my code to get the expected result