Issue
I want to set this.arr.items to 1 when this.inputObject.timeTable will return values from the map to me, e.g .: I will give an example because it is difficult to explain it to me For example:
"timeTable":{
"0": [{"from":"08:00","to":"12:00"}, {"from":"14:00","to":"16:00"}],
"1": [{"from":"00:00","to":"05:00"}]
"2": [],
"3": []
"4": [],
"5": []
"6": []
}
i want to set this.arr.items:
this.arr.items= [
[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
];
My code:
arr: Row[] = [
{ items: new Array(24).fill(1),active: true},
{ items: new Array(24).fill(1),active: true},
{ items: new Array(24).fill(1),active: true},
{ items: new Array(24).fill(1),active: true},
{ items: new Array(24).fill(1),active: true},
{ items: new Array(24).fill(1),active: true},
{ items: new Array(24).fill(1),active: true},
];
if(this.inputObject.timeTable){
for (let [key, value] of this.inputObject.timeTable.entries()) {
this.arr.forEach(el=>{
el.items.fill(0)
el.active=false
})
value.forEach((el,ind)=>{
for(let i = parseInt(el.from); i<parseInt(el.to)+1; i++){
this.arr[key].items[i] = 1
}
})
}
}
Unfortunately it doesn't work All my code: https://stackblitz.com/edit/angular-ivy-2lveus?file=src%2Fapp%2Fapp.component.ts&fbclid=IwAR23d8PtUNJpMAOmqvFbe3PAQQhtNzydHSfSQv_UEsi7E3DKMPgv6PffXqg
Solution
You can get the hour part from from and to key for each object and then replace the value to 1 for every value in that range.
const timeTables = { "0": [{"from":"08:00","to":"12:00"}, {"from":"14:00","to":"16:00"}], "1": [{"from":"00:00","to":"05:00"}], "2": [], "3": [], "4": [], "5": [], "6": [] },
getHours = (time) => parseInt(time.split(':')[0], 10)
result = Object.entries(timeTables).reduce((r, [key, value], i) => {
r[i] = Array(24).fill(0);
value.forEach(o => {
let start = getHours(o.from);
const end = getHours(o.to);
while(start <= end) {
r[i][start] = 1;
start++;
}
})
return r;
}, []);
console.log(result.map(arr => arr.join(',')).join('\n'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answered By - Hassan Imam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.