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

147
Views
Transforming data into paths

I have a data like this:

[
  {
    user_id: 356793,
    bets: [
      {
        key: 'total',
        team: {
          away: 1959,
          home: 1418
        },
        value: 1.76,
        match_game: {
          score: {
            away: 17,
            home: 18
          }
        }
      }
    ]
  }
]

And I want to transform it into paths or just log the paths, like this:

data[i].user_id
data[i].bets
data[i].bets[i]
data[i].bets[i].key
data[i].bets[i].team
data[i].bets[i].team.away
data[i].bets[i].team.home
data[i].bets[i].value
data[i].bets[i].match_game
data[i].bets[i].match_game.score
data[i].bets[i].match_game.score.away
data[i].bets[i].match_game.score.home

I think recursion does the trick here but it throws a RangeError: Maximum call stack size exceeded. And I don't know where it fails. It works on the first array but on the second array, it fails and causes an infinite loop.

Here's my code:

const data = [
  {
    user_id: 356793,
    bets: [
      {
        key: 'total',
        team: {
          away: 1959,
          home: 1418
        },
        value: 1.76,
        match_game: {
          score: {
            away: 17,
            home: 18
          }
        }
      }
    ]
  }
]

const isArray = hit => Array.isArray(hit)
const isObject = hit => !isArray(hit) && typeof hit === 'object'
const isNumber = hit => typeof hit === 'number'
const isString = hit => typeof hit === 'string'
const isNonIterable = hit => !isArray(hit) && !isObject(hit)

const toPaths = (payload, prefix) => {
  if (isArray(payload)) {
    for (let i = 0; i < data.length; i++) {
      const currData = data[i]

      toPaths(currData, prefix + '[i]')
    }

    return
  }

  if (isObject(payload)) {
    for (const key in payload) {
      const currData = payload[key]

      if (isNonIterable(currData)) {
        console.log(`${prefix}.${key}`)
        continue
      }

      if (isArray(currData)) {
        for (let i = 0; i < data.length; i++) {
          const currData = data[i]

          toPaths(currData, prefix + '[i]')
        }

        continue
      }

      // console.log(`${prefix}.key`)
    }

    return console.log(prefix)
  }
}

toPaths(data, 'bets')

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In two places, you iterate over data while you were supposed to use payload:

for (let i = 0; i < data.length; i++) {
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!