當 this.inputObject.timeTable 將值從地圖回傳給我時,我想將 this.arr.items 設定為 1,例如。:我將舉一個例子,因為很難向我解釋它例如:
"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": []
}
我想設定 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],
];
我的代碼:
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
}
})
}
}
不幸的是它不起作用我所有的代碼:https : //stackblitz.com/edit/angular-ivy-2lveus?file=src/app/app.component.ts&fbclid=IwAR23d8PtUNJpMAOmqvFbe3PAQQhtNzydHSfSQv_UEsi7E3DKMPgv6P
uj5u.com熱心網友回復:
你可以得到的小時部分從from與to鍵每個物件,然后替換值1在此范圍內的每一個值。
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; }
uj5u.com熱心網友回復:
基于from和to通過spread operator ...或連接時隙陣列Array.concat。
const config = {"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": []
}}
function getTimeArray(timeTable) {
const emptyArray = Array.from({length: 24}).map(() => 0)
return Object.entries(timeTable).map(([key, value]) => {
let result = emptyArray.slice()
value.forEach((item) => {
const beg = item.from.split(':')[0] * 1
const to = item.to.split(':')[0] * 1
result = [...result.slice(0, beg), ...result.slice(beg, to 1).map(() => 1), ...result.slice(to 1)]
})
return result
})
}
console.log(
JSON.stringify(getTimeArray(config.timeTable))
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/381760.html
標籤:javascript 有角的 打字稿
