Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda