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

198
Views
Nested Maps for Next getStaticPaths Params

Imagine your data structure is like :

data = {
  "cse": {
    "first": [
      "Computer Hardware Essentials",
      "Computer System Essentials",
      "Cultural Education"
    ],
    "second": [
      "Discrete Mathematics",
      "Linear Algebra",
      "Engineering Physics - A",
    ],
    "third": [
      "Numerical Methods",
      "Optimization Techniques",
      "Digital Electronics",
    ],
    "fourth": [
      "Probability and Random Processes",
      "Data Structures and Algorithms",
      "Theory of Computation"
   ]
     }
   }

And you have the following route /[semester]/[subject] so the Next.js structure folder is: [semester]/[subject].js

How will we integrate through the data so that we can get the getStaticPaths params in a single nested loop?

Required Data :

[
{ params: {semester: "first", subject: "computer-hardware-essentials"}},
{ params: {semester: "first", subject: "computer-system-essentials"}},
{ params: {semester: "first", subject: "cultutal-education"}},
{ params: {semester: "second", subject: "discrete-mathemetics"}},
{ params: {semester: "second", subject: "linear-algebra"}},
{ params: {semester: "second", subject: "engineering-physics-a"}},
{ params: {semester: "third", subject: "numerical-methods"}},
{ params: {semester: "third", subject: "optimization-techniques"}},
{ params: {semester: "third", subject: "digital-electronics"}},
{ params: {semester: "fourth", subject: "probability-and-random-process"}},
{ params: {semester: "fourth", subject: "data-structure-and-algorithms"}},
{ params: {semester: "fourth", subject: "theory-of-computation"}}
]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Loop through the object using for...in and then loop the inner array using forEach to add each subject to an array:

const results = [];
for (const x in data.cse) {
  data.cse[x].forEach((s) => {
    results.push({
      params: {
        semester: x,
        subject: s
      }
    });
  });
}

const data = {
  "cse": {
    "first": [
      "Computer Hardware Essentials",
      "Computer System Essentials",
      "Cultural Education"
    ],
    "second": [
      "Discrete Mathematics",
      "Linear Algebra",
      "Engineering Physics - A",
    ],
    "third": [
      "Numerical Methods",
      "Optimization Techniques",
      "Digital Electronics",
    ],
    "fourth": [
      "Probability and Random Processes",
      "Data Structures and Algorithms",
      "Theory of Computation"
    ]
  }
}

const results = [];
for (const x in data.cse) {
  data.cse[x].forEach((s) => {
    results.push({
      params: {
        semester: x,
        subject: s
      }
    });
  });
}

console.info(results);

about 4 years ago · Juan Pablo Isaza Report

0

Using functional methods you can loop over entries of data.cse and then chain with .flatMap

const result = Object.entries(data.cse).flatMap(([key, value]) => {
  return value.map(i => {
    return { 
      params: {
        semester: key,
        subject: i.toLowerCase().replaceAll(' ', '-')
      }
    }
  })
})
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!