Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!