Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

62
Views
Is there a better way, to iterate through

How to iterate in a better way through A and B?

Data structure A:

AbsatzNrSaetzeText: { [key: string]: string[] };

Data structure B:

 AbsatzNrSaetzeText: Record<number, string[]>;

Is there a better way, to get that value:

     for (var i = 0; i < this.UI_Seiten.length; i++) {
      for (var j in this.UI_Seiten[i].AbsatzNrSaetzeText) {
        for (var k = 0; k < this.UI_Seiten[i].AbsatzNrSaetzeText[j.valueOf()].length; k++) {
          console.log(this.UI_Seiten[i].AbsatzNrSaetzeText[j.valueOf()][k]);
        }
      }
    }
7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

For iterable objects (like arrays), there is no need to reference the index in order to iterate over the values - for..of allows for significantly cleaner code, by putting the value being iterated over directly into an identifier that can be used in later loops.

Use Object.values instead of for..in, since you don't care about the key on the object, only about the value.

for (const seiten of this.UI_Seiten) {
    for (const arr of Object.values(seiten.AbsatzNrSaetzeText)) {
        for (const str of arr) {
            console.log(str);
        }
    }
}
7 months 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 job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.