Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

112
Visualizações
creating an object with hours through the user interface

I am having trouble creating the right object. I have a calendar that stores the hours. He wants timeTable to hold such an object, for example

"timeTable":{
    "0": [{"from":"08:00","to":"12:00"}, {"from":"14:00","to":"18:00"}],
    "1": [{"from":"08:00","to":"16:00"}]
  }

How, after clicking the save button, send such an object to:

 timeTable: Map<number, Array<HourScheduleDefinitionModel>>;

interface:

interface HourScheduleDefinitionModel {
  from: string;
  to: string;
}

https://stackblitz.com/edit/angular-ivy-e92ezv?file=src%2Fapp%2Fapp.component.ts

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

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;  
}

example data :-

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
  }
]

result :-

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' } ]
  }
]
about 4 years ago · Santiago Gelvez Relatório

0

Here's a working example of adding a new map to your object storage. Please note that the timeTable type is not used in the code as the comment of @AluanHaddad is the same as what I was trying to explain.

    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)

Playground

about 4 years ago · Santiago Gelvez Relatório

0

I think you want an array (indexed by numbers) instead of an object (indexed by strings):

TS Playground link

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]);
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda