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

145
Vistas
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 Respuestas
Responde la pregunta

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 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