const isArray = hit => Array.isArray(hit)
const isObject = hit => !isArray(hit) && typeof hit === 'object'
const isNonIterable = hit => !isArray(hit) && !isObject(hit)
const isBoolean = hit => typeof hit === 'boolean'
const isNumber = hit => typeof hit === 'number' || (!isNaN(Number(hit)) && !isBoolean(hit))
const isString = hit => typeof hit === 'string'
const isDate = hit => new Date(hit).toString() !== 'Invalid Date'
const getType = payload => {
if (isArray(payload)) {
return 'array'
}
if (isObject(payload)) {
return 'object'
}
if (isNumber(payload)) {
return 'number'
}
if (isDate(payload)) {
return 'date'
}
if (isString(payload)) {
return 'string'
}
if (isBoolean(payload)) {
return 'boolean'
}
if (isBoolean(payload)) {
return 'boolean'
}
}
const toPath = (data, prefix = '') => {
const store = []
const storeData = (loggerPayload, loggerPrefix) => ({
path: loggerPrefix,
type: getType(loggerPayload)
})
if (isArray(data)) {
if (prefix) {
store.push(storeData(data, prefix))
}
store.push(...toPath(data[0], `${prefix}[i]`))
return store
}
if (isObject(data)) {
if (prefix) {
store.push(storeData(data, prefix))
}
for (const key in data) {
const currData = data[key]
const currPrefix = prefix ? `${prefix}.${key}` : key
isNonIterable(currData)
? store.push(storeData(currData, currPrefix))
: store.push(...toPath(currData, currPrefix))
}
return store
}
return store
}
const data = [
{
match: {
id: 720359,
play: '2021-12-20 00:30:00',
score: null,
status: 'Postponed'
},
league: {
id: 1115,
kor: '',
name: 'Norway: Blno'
},
home_team: {
id: 1610,
kor: '',
name: 'Tromso'
},
away_team: {
id: 1613,
kor: '',
name: 'Avangard Omsk'
},
game: {
id: 3,
kor: '농구',
name: 'basketball'
},
match_games: [
{
type: {
id: 1,
div: 'odd',
kor: '승/무/패',
name: '3Way Result'
},
odds: {
odd: {
size: 1,
Home: 2.95,
deep: 1,
stop: 0,
gap: 1.45,
Draw: 13,
Away: 1.5
}
},
score: '[83,60]'
}
]
}
]
const paths = toPath(data)
console.log(paths)
// [
// {
// "path": "[i]",
// "type": "object"
// },
// {
// "path": "[i].match",
// "type": "object"
// },
// {
// "path": "[i].match.id",
// "type": "number"
// },
// {
// "path": "[i].match.play",
// "type": "date"
// },
// {
// "path": "[i].match.score",
// "type": "object"
// },
// {
// "path": "[i].match.status",
// "type": "string"
// },
// {
// "path": "[i].league",
// "type": "object"
// },
// {
// "path": "[i].league.id",
// "type": "number"
// },
// {
// "path": "[i].league.kor",
// "type": "number"
// },
// {
// "path": "[i].league.name",
// "type": "string"
// },
// {
// "path": "[i].home_team",
// "type": "object"
// },
// {
// "path": "[i].home_team.id",
// "type": "number"
// },
// {
// "path": "[i].home_team.kor",
// "type": "number"
// },
// {
// "path": "[i].home_team.name",
// "type": "string"
// },
// {
// "path": "[i].away_team",
// "type": "object"
// },
// {
// "path": "[i].away_team.id",
// "type": "number"
// },
// {
// "path": "[i].away_team.kor",
// "type": "number"
// },
// {
// "path": "[i].away_team.name",
// "type": "string"
// },
// {
// "path": "[i].game",
// "type": "object"
// },
// {
// "path": "[i].game.id",
// "type": "number"
// },
// {
// "path": "[i].game.kor",
// "type": "string"
// },
// {
// "path": "[i].game.name",
// "type": "string"
// },
// {
// "path": "[i].match_games",
// "type": "array"
// },
// {
// "path": "[i].match_games[i]",
// "type": "object"
// },
// {
// "path": "[i].match_games[i].type",
// "type": "object"
// },
// {
// "path": "[i].match_games[i].type.id",
// "type": "number"
// },
// {
// "path": "[i].match_games[i].type.div",
// "type": "string"
// },
// {
// "path": "[i].match_games[i].type.kor",
// "type": "string"
// },
// {
// "path": "[i].match_games[i].type.name",
// "type": "string"
// },
// {
// "path": "[i].match_games[i].odds",
// "type": "object"
// },
// {
// "path": "[i].match_games[i].odds.odd",
// "type": "object"
// },
// {
// "path": "[i].match_games[i].odds.odd.size",
// "type": "number"
// },
// {
// "path": "[i].match_games[i].odds.odd.Home",
// "type": "number"
// },
// {
// "path": "[i].match_games[i].odds.odd.deep",
// "type": "number"
// },
// {
// "path": "[i].match_games[i].odds.odd.stop",
// "type": "number"
// },
// {
// "path": "[i].match_games[i].odds.odd.gap",
// "type": "number"
// },
// {
// "path": "[i].match_games[i].odds.odd.Draw",
// "type": "number"
// },
// {
// "path": "[i].match_games[i].odds.odd.Away",
// "type": "number"
// },
// {
// "path": "[i].match_games[i].score",
// "type": "string"
// }
// ]