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

131
Vistas
How to loop through object of strings with dates in typescript?

I have object, keys are dates, before dot are months, after are years:

const dataObj = {
  '01.2015': 0.01,
  '02.2015': 0.10,
  '03.2015': 0.05,
  '04.2015': 0.25,
  // ...
}

the function takes two dates and return an array with objects containing values from the dataObj

interface Point {
  date: string;
  value: number;
}

type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type Day = `${Digit}${Digit}`;
type Month =
  | '01'
  | '02'
  // ...
  | '11'
  | '12';

type Year = '2015' | '2016' | '2017';
type DateString = `${Day}.${Month}.${Year}`;

type MonthDateString = `${Month}.${Year}`;

function count(firstDate: DateString, secondDate: DateString): Point[] {
 const result: Point[] = [];
 let currentDate = firstDate.slice(3) as MonthDateString;
 // loop through dataObj and push in result
 return result // [{date: '01.2015', value: 0.01}, {...}, ...]
}

How to loop through dataObj? I tried for loop

for(let i = currentDate; i !== secondDate.slice(3);  )

but have no idea what to do with final-expression, how to update counter variable, pass next key(string with date) from dataObj.

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

0

Some examples below:

for (let key in dataObj) {
   console.log(key, dataObj[key]);
}

for (let [key, value] of Object.entries(dataObj)) {
   console.log(key, value);
}

If you want to have and update counter variable:

let keys = Object.keys(dataObj);

for (let i = 0; i < keys.length; i++) {
    console.log(i, keys[i], dataObj[keys[i]]);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

There is some confusion in regards to the expected output. The function count needs to return a Point array which is an object array where each object has two props namely date (a string) and value (number, floating-point per inline-comments in the code).

It is assumed that the OP requires to find key-value pairs in dataObj such that the key (ie, month-year) is between firstDate and secondDate (which are parameters to count function).

Based on this assumption, the below code may achieve the desired objective (to generate an array of Point elements).

const isGreaterOrEqual = (dtOne: MonthDateString, dtTwo: MonthDateString) : Boolean => {
    const getYear = (dt: MonthDateString) : string => dt.slice(3);
    const getMonth = (dt: MonthDateString) : string => dt.substring(0, 2);
    return ((getYear(dtOne) > getYear(dtTwo)) ||
        (getYear(dtOne) === getYear(dtTwo) &&
            getMonth(dtOne) >= getMonth(dtTwo)
        )
    );
};

function count(firstDate: DateString, secondDate: DateString): Point[] {
 let beginDate = firstDate.slice(3) as MonthDateString;
 let endDate = secondDate.slice(3) as MonthDateString;
 return (
     Object.entries(dataObj)
     .filter(([k, v]) => isGreaterOrEqual(k as MonthDateString, beginDate) && isGreaterOrEqual(endDate, k as MonthDateString))
     .map(([k, v]) => ({ date: k, value: v} as Point))
    );
};

console.log(count( "01.01.2015" as DateString, "01.02.2015" as DateString));

TS Playground: Link here

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