I am working on app and I want to show 3 columns
date, min and zone
I am getting an array of objects
[
{date: 2022-12-28T07:37:16.859Z, min: 2, zone: zone A},
{date: 2022-12-28T07:38:13.859Z, min: 1, zone: zone B},
{date: 2022-12-28T07:36:15.859Z, min: 3, zone: zone C},
{date: 2022-12-31T07:37:16.859Z, min: 2, zone: zone E}
{date: 2022-12-25T07:37:16.859Z, min: 4, zone: zone D}
]
I want to cover condition as below:
- If date is repeating then it should be repeated only once.
- min with same date should be addition like (2 1 3=6)
- zone with same date should be concat like (Zone A, Zone B, Zone C)
and want result like below:
[
{date: 2022-12-28T07:37:16.859Z, min: 6, zone: zone A, zone B, zone C},
{date: 2022-12-31T07:37:16.859Z, min: 2, zone: zone E}
{date: 2022-12-25T07:37:16.859Z, min: 4, zone: zone D}
]
any help is apricated.
CodePudding user response:
Filter out elements with a duplicate date, and when you remove them add/concat the min/zone values to the remaining object's respective values.
CodePudding user response:
First, you need to format date according to manage condition in code
a = [
{date: "2022-12-28", min: 2, zone: "zone A"},
{date: "2022-12-28", min: 1, zone: "zone B"},
{date: "2022-12-28", min: 3, zone: "zone C"},
{date: "2022-12-31", min: 2, zone: "zone E"},
{date: "2022-12-25", min: 4, zone: "zone D"}
]
let h = []
a.forEach(item => {
let data = h.find(e => e.date == item.date);
if(data){
data.min = data.min item.min;
data.zone = data.zone ", " item.zone;
let index = h.findIndex(e => e.date == item.date);
h[index] = data
}else
h.push(item)
})
console.log(h)
CodePudding user response:
The desired result requires grouped by date (with Date only).
You can work with .reduce() to group the data by date.
When there is no element with
datefound in the group, you need to create an object withkandv. Next push it into the accumulator (acc) array.When there is an element with
datematch in the existing group, you need to update the existing element in theaccarray for theminandzone.
Lastly, you need to grab all the v values from the groupedDate array.
let input = [
{ date: "2022-12-28T07:37:16.859Z", min: 2, zone: "zone A" },
{ date: "2022-12-28T07:38:13.859Z", min: 1, zone: "zone B" },
{ date: "2022-12-28T07:36:15.859Z", min: 3, zone: "zone C" },
{ date: "2022-12-31T07:37:16.859Z", min: 2, zone: "zone E" },
{ date: "2022-12-25T07:37:16.859Z", min: 4, zone: "zone D" }
];
let groupedDate = input.reduce(
(acc: { k: string, v: { date: string; min: number; zone: string[] } }[],
cur: { date: string, min: number, zone: string }) => {
let date = new Date(cur.date);
let dateString = `${date.getFullYear()}-${date.getMonth() 1}-${date.getDate()}`;
let index = acc.findIndex(x => x.k == dateString);
if (index > -1) {
acc[index].v.min = cur.min;
acc[index].v.zone.push(cur.zone);
} else {
acc.push({
k: dateString,
v: { date: cur.date, min: cur.min, zone: [cur.zone] }
});
}
return acc;
}, []);
let result = groupedDate.map(x => x.v);
CodePudding user response:
you can use the Array.map() method to create a new array of objects that combine a JavaScript Date object with other properties.
let dateArray = [new Date(), new Date(), new Date()];
let dataArray = dateArray.map(date => {
return {
date: date,
value: Math.random()
}
});
