Q: 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')



A: In two places, you iterate over data while you were supposed to use payload:
for (let i = 0; i < data.length; i++) {

