I have a JSON data like below
[ {"id" : "1", "category" : "A", "Date" : "2000-02-20"}, {"id" : "2", "category" : "B", "Date" : "2000-03-11"}, {"id" : "3", "category" : "C", "Date" : "2000-05-11"}, .. .. .. {"id" : "4", "category" : "C", "Date" : "2010-01-20"}, {"id" : "5", "category" : "A", "Date" : "2010-04-02"}, {"id" : "6", "category" : "B", "Date" : "2010-06-10"}, ... ... .. {"id" : "998", "category" : "C", "Date" : "2022-08-14"}, {"id" : "999", "category" : "B", "Date" : "2022-09-15"}, {"id" : "1000", "category" : "A", "Date" : "2022-07-10"} ]
I need the total count of records data of each category to be displayed in year wise table format like below based on time period in between 2 input dates, only for the years in that time period not for all the years available in data.
For example if i input dates as 2020-05-15 and 2022-05-30 it should be like below.
I am struggling to write the logic dynamically generate year wise tables only for the selected dates, please help me to get the solution for it.
CodePudding user response:
This is an example of filtering by the date.
const values = [{ "id": "1", "category": "A", "Date": "2000-02-20" }, { "id": "2", "category": "B", "Date": "2000-03-11" }, { "id": "3", "category": "C", "Date": "2000-05-11" }, { "id": "4", "category": "C", "Date": "2010-01-20" }, { "id": "5", "category": "A", "Date": "2010-04-02" }, { "id": "6", "category": "B", "Date": "2010-06-10" }, { "id": "998", "category": "C", "Date": "2022-08-14" }, { "id": "999", "category": "B", "Date": "2022-09-15" }, { "id": "1000", "category": "A", "Date": "2022-07-10" }]
const calcRecords = (begin, end) => {
let setBegin = new Date(begin);
let setEnd = new Date(end);
console.log(setEnd);
//If you need to know the value of it id's
console.log(values.filter((val) => {
const recordDate = new Date(val.Date);
//console.log(recordDate);
if (setBegin < recordDate && recordDate < setEnd ) return true;
return false;
}).reduce((previousValue, currentValue) => Number(previousValue.id) Number(currentValue.id)))
//If you need to know how many elements were found
console.log(values.filter((val) => {
const recordDate = new Date(val.Date);
//console.log(recordDate);
if (setBegin < recordDate && recordDate < setEnd ) return true;
return false;
}).length)
}
calcRecords('2000-02-20', '2010-01-20');
CodePudding user response:
Here's another example where you do this by the category
const values = [{ "id": "1", "category": "A", "Date": "2000-02-20" }, { "id": "2", "category": "B", "Date": "2000-03-11" }, { "id": "3", "category": "C", "Date": "2000-05-11" }, { "id": "4", "category": "C", "Date": "2010-01-20" }, { "id": "5", "category": "A", "Date": "2010-04-02" }, { "id": "6", "category": "B", "Date": "2010-06-10" }, { "id": "998", "category": "C", "Date": "2022-08-14" }, { "id": "999", "category": "B", "Date": "2022-09-15" }, { "id": "1000", "category": "A", "Date": "2022-07-10" }]
const calcRecords = (begin, end) => {
let setBegin = new Date(begin);
let setEnd = new Date(end);
const filteredData = values.filter((val) => {
const recordDate = new Date(val.Date);
//console.log(recordDate);
if (setBegin <= recordDate && recordDate <= setEnd ) return true;
return false;
})
//This gets the categories and removes duplicates
const categories = [... new Set(filteredData.map(record => record.category))];
categories.forEach(cat => {
//How many entries this category has
console.log(`Category ${cat} has ${filteredData.filter(record => record.category === cat).length} items`)
//Calculates the sum of the id's
console.log(`Category ${cat} sum of Id's is ${filteredData.filter(record => record.category === cat).reduce((previousValue, currentValue) => previousValue parseInt(currentValue.id), 0)} items`)
});
}
calcRecords('2000-02-20', '2022-07-10');