got a array of objects and need to filter this by 2 criterias. One of those criterias is an index counting up.
let data =
[{"hour": 1, "dayIndex": 0, "value": "something"},
{"hour": 1, "dayIndex": 1, "value": "something"},
{"hour": 1, "dayIndex": 3, "value": "something"},
{"hour": 2, "dayIndex": 0, "value": "something"},
{"hour": 2, "dayIndex": 1, "value": "something"},
// and so on
]
I need an array of objects filtered by "hour" and ascending "dayIndex" and it is important that for missing dayIndexes an empty object is created. So for hour=1 I would need this:
let hourOneArray =
[
{"hour" : 1, "dayIndex": 0, "value": "something"},
{"hour" : 1, "dayIndex": 1, "value": "something"},
{}, //empty because dayIndex 2 is missing
{"hour" : 1, "dayIndex": 3, "value": "something"},
{}, //empty because dayIndex 4 is missing
]
My approach was:
for(let i = 0; i < 4; ++i){
hourOneArray = data.filter((arg) => {
return ((arg.hour === 1) && (arg.dayIndex === i));
})
}
Thanks in advance