Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
How to use string as key for array in TypeScript

I have a TypeScript problem:

let classStart: keyof typeof monday;
const now = new Date();
console.log(now.getHours());
for (let i = Object.keys(monday).length - 1; i > 0; i--) {
  let time: any = Object.keys(monday)[i].split(":");
  // eslint-disable-next-line eqeqeq
  if (time[0] == now.getHours()) {
    if (time[1] <= now.getMinutes()) {
      console.log(Object);
      classStart = Object.keys(monday)[i];
      console.log();
    } else {
      //classStart = Object.keys(monday)[i - 1];
      console.log(Object.keys(monday)[i - 1]);
    }

    // console.log(monday[classStart]);
  }
}

This is my code, I need to use the Object.keys(monday)[i] (which is a string) as a property for monday[].

If I use the let classStart: keyof typeof monday; I can assign e.x "7:25" to classStart but I can't assign it using a function like Object.keys(monday)[i];. Why?

The JSON where I get the data from looks like this:

    ...
    "7:25": ["B1", "B2", "B3","B4"],
    ...

It is a typescript problem. I'm quite sure.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think the main problem, that no one can guess what is the type of monday, (TypeScript too). So if you specify its type, the problem will become much more clearer

I suspect that

monday: {
   [HHMM:string]: string[]
}

So I slightly altered your code

const mondayInput = {
    "10:13": ["B1"],
    "7:25": ["B1", "B2", "B3","B4"]
};
const monday = mondayInput as Record<string,string[]>

//fixed date time for test proposes
let now = new Date("2013/01/01 10:10:00")
console.log(now, now.getHours(),  now.getMinutes());

function HHMMtoInt(x:string){const [HH, MM] = x.split(":"); return parseInt(HH) * 100 + parseInt(MM)}
const times = Object.keys(monday).sort((a,b) => HHMMtoInt(a)-HHMMtoInt(b));

for (let i = 0;i < times.length; i++) {
  const  timeHHMM = times[i]; 

  let time = timeHHMM.split(":").map(x => parseInt(x));

  let timeInterval: number | undefined;
  console.log("Compare", time[0], time[1], now.getHours(), now.getMinutes())
  if (time[0] == now.getHours()) {
    if (time[1] <= now.getMinutes()) {
        timeInterval = i;
    } else {
        timeInterval = i - 1; 
    }
  }
  
  if(timeInterval !== undefined && timeInterval >= 0 ){
      console.log("Time", times[timeInterval], "Classes", monday[times[timeInterval]] );
  }
}

Playground Link: Provided

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda