I'm having a problem grouping this data using javascript. Just starting my career using pure javascript and nodeJS for building a website application.
here's my data result using typeORM.
let data = [
{
control_no: '01-55-10-021-000001',
date_created: '2023-01-05',
validated: '1',
date_validated: '2023-01-06',
submitted: '1',
date_submitted: '2023-01-07'
},
{
control_no: '01-55-10-016-000190',
date_created: '2023-01-05',
validated: '1',
date_validated: '2023-01-06',
submitted: '0',
date_submitted: null
},
{
control_no: '01-55-10-001-000001',
date_created: '2023-01-06',
validated: '0',
date_validated: null,
submitted: '0',
date_submitted: null
},
{
control_no: '01-55-10-001-000002',
date_created: '2023-01-06',
validated: '0',
date_validated: null,
submitted: '0',
date_submitted: null
},
{
control_no: '01-55-10-001-000003',
date_created: '2023-01-06',
validated: '0',
date_validated: null,
submitted: '0',
date_submitted: null
},
{
control_no: '01-55-10-001-000004',
date_created: '2023-01-06',
validated: '1',
date_validated: '2023-01-07',
submitted: '1',
date_submitted: '2023-01-08'
},
{
control_no: '01-55-10-001-000005',
date_created: '2023-01-06',
validated: '1',
date_validated: '2023-01-07',
submitted: '0',
date_submitted: null
},
{
control_no: '01-55-10-001-000006',
date_created: '2023-01-06',
validated: '1',
date_validated: '2023-01-07',
submitted: '1',
date_submitted: '2023-01-08'
},
{
control_no: '01-55-10-001-000007',
date_created: '2023-01-09',
validated: '0',
date_validated: null,
submitted: '0',
date_submitted: null
},
{
control_no: '01-55-10-001-000008',
date_created: '2023-01-09',
validated: '0',
date_validated: null,
submitted: '0',
date_submitted: null
}
];
now i want to have this structure of data where i want to count all data as created then count all validated data as validated and all tagged as submitted as submitted by date
let created = [
{
date: '2023-01-05',
count: 2
},
{
date: '2023-01-06',
count: 6
},
{
date: '2023-01-09',
count: 2
}
];
let validated = [
{
date: '2023-01-06',
count: 2
},
{
date: '2023-01-07',
count: 3
}
]
let submitted = [
{
date: '2023-01-07',
count: 1
},
{
date: '2023-01-08',
count: 2
}
]
then sort by date.
i'm not that good in manipulating arrays using javascript, been in PHP in so many years.
CodePudding user response:
You can do this by using 1 reduce.But I am using 3 different reduce function
- For Created:
let created = data.reduce((acc, curr) => {
let key = curr.date_created;
if (!acc[key]) {
acc[key] = {
date: key,
count: 1
};
} else {
acc[key].count ;
}
return acc;
}, {});
// convert the object to array
created = Object.values(created);
// sort data by date
created.sort((a,b) => new Date(a.date) - new Date(b.date));
console.log(created)
- for validated
let validated = data.reduce((acc, curr) => {
let key = curr.date_validated;
if (!acc[key]) {
acc[key] = {
date: key,
count: 1
};
} else {
acc[key].count ;
}
return acc;
}, {});
// convert the object to array
validated = Object.values(validated);
// remove the null value
validated = validated.filter(val => val.date != null)
// sort the data by date
validated.sort((a,b) => new Date(a.date) - new Date(b.date));
3.For submitted
let submitted = data.reduce((acc, curr) => {
let key = curr.date_submitted;
if (!acc[key]) {
acc[key] = {
date: key,
count: 1
};
} else {
acc[key].count ;
}
return acc;
}, {});
// convert the object to array
submitted = Object.values(submitted);
// remove the null values
submitted = submitted.filter(val => val.date != null)
//sort the data by date
submitted.sort((a, b) => new Date(a.date) - new Date(b.date));
If you still face any issue. Please let me know