我無法創建正確的物件。我有一個存盤小時的日歷。他想讓timeTable保存這樣一個物件,例如
"timeTable":{
"0": [{"from":"08:00","to":"12:00"}, {"from":"14:00","to":"18:00"}],
"1": [{"from":"08:00","to":"16:00"}]
}
單擊保存按鈕后,如何將這樣的物件發送到:
timeTable: Map<number, Array<HourScheduleDefinitionModel>>;
界面:
interface HourScheduleDefinitionModel {
from: string;
to: string;
}
https://stackblitz.com/edit/angular-ivy-e92ezv?file=src/app/app.component.ts
uj5u.com熱心網友回復:
const getTimeline = () => {
result = []
for(item of data){
var start = -1, timeline = [];
for(var i = 0; i < item.items.length; i ){
if(item.items[i] === 1){
if(start === -1) {
start = i;
}
}else{
if(start !== -1){
timeline.push({"from": start < 10 ? "0" start ":00" : start ":00" , "to": i < 10 ? "0" i ":00" : i ":00"});
start = -1;
}
}
if(start !== -1 && i === item.items.length - 1){
timeline.push({"from": start < 10 ? "0" start ":00" : start ":00" , "to": "00:00"});
}
}
result.push({
day: item.name,
timeline : timeline
})
}
return result;
}
示例資料:-
data = [
{
name: 'Monday',
items: [0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
active: true
},
{
name: 'Tuesday',
items: [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
active: true
}
]
結果 :-
console.log(getTimeline(data));
[
{
day: 'Monday',
timeline: [
{ from: '01:00', to: '02:00' },
{ from: '03:00', to: '04:00' },
{ from: '20:00', to: '00:00' }
]
},
{
day: 'Tuesday',
timeline: [ { from: '01:00', to: '04:00' }, { from: '20:00', to: '21:00' } ]
}
]
uj5u.com熱心網友回復:
這是將新地圖添加到物件存盤的作業示例。請注意,timeTable代碼中未使用該型別,因為@AluanHaddad的注釋與我試圖解釋的相同。
var hours = {"timeTable":{
"0": [{"from":"08:00","to":"12:00"}, {"from":"14:00","to":"18:00"}],
"1": [{"from":"08:00","to":"16:00"}]
}};
interface HourScheduleDefinitionModel {
from: string;
to: string;
}
var newHour: HourScheduleDefinitionModel = {
from: "09:00",
to: "10:00"
}
var key = 2;
var value = [newHour];
hours.timeTable[key.toString()] = value
console.log(hours)
操場
uj5u.com熱心網友回復:
我認為你想要一個陣列(由數字索引)而不是一個物件(由字串索引):
TS 游樂場鏈接
interface HourScheduleDefinitionModel {
from: string;
to: string;
}
type TimeTable = (HourScheduleDefinitionModel[])[];
const tt: TimeTable = [
[{"from":"08:00","to":"12:00"}, {"from":"14:00","to":"18:00"}], // 0
[{"from":"08:00","to":"16:00"}], // 1
];
console.log(tt[0]);
console.log(tt[1]);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/379935.html
標籤:javascript 有角的 打字稿
