Last active
April 7, 2017 18:42
-
-
Save ronaiza-cardoso/58a4deacbb9d24956eba62f1dcd08c4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": "13/04", | |
"dia": "Qui", | |
"inicio": "16:24", | |
"inicio_intervalo": null, | |
"fim_intervalo": null, | |
"fim": "20:55", | |
"jornada": "05:01", | |
"ocorrencia": null, | |
"folga": null, | |
"veiculo": null, | |
"viagens": [ | |
{ | |
"saida": "00:00", | |
"chegada": "00:00", | |
"local_origem": "RODOVIARIA DE RIO BONITO / RBO", | |
"local_destino": "RODOVIARIA DE SAQUAREMA / SAQ", | |
"sentido": "ida" | |
}, | |
{ | |
"saida": "00:00", | |
"chegada": "00:00", | |
"local_origem": "RODOVIARIA DE RIO BONITO / RBO", | |
"local_destino": "RODOVIARIA DE SAQUAREMA / SAQ", | |
"sentido": "volta" | |
} | |
] | |
} | |
this.viagens = this.detalhesEscalaModal.viagens // funcionou, o indice se transformou em array, mas | |
this.viagens = Array.from(this.viagens) | |
//isso não funcionou | |
this.detalhesEscalaModal = Array.from(this.detalhesEscalaModal) | |
// preciso de um novo objeto com esses valores: | |
let inicialized = Object.keys(this.detalhesEscalaModal).map(k => { | |
return { | |
dia: k, | |
inicio: k, | |
inicio_intervalo: k, | |
fim_intervalo: k, | |
fim: k, | |
jornada: k, | |
ocorrencia: k, | |
folga: k, | |
veiculo: k } | |
}) | |
this.detalhesEscalaModal = Object.keys(this.detalhesEscalaModal)
.reduce((acc, key) => ([...acc, { key, value: this.detalhesEscalaModal[key] }]), [])
console.log(this.detalhesEscalaModal)
by @vinicius73
Resolvi assim, mas ainda irei refatorar p/ colocar nome nas coisas:
const obj = {
"data": "13/04",
"dia": "Qui",
"inicio": "16:24",
"inicio_intervalo": null,
"fim_intervalo": null,
"fim": "20:55",
"jornada": "05:01",
"ocorrencia": null,
"folga": null,
"veiculo": null,
"viagens": [{
"saida": "00:00",
"chegada": "00:00",
"local_origem": "RODOVIARIA DE RIO BONITO / RBO",
"local_destino": "RODOVIARIA DE SAQUAREMA / SAQ",
"sentido": "ida"
}, {
"saida": "00:00",
"chegada": "00:00",
"local_origem": "RODOVIARIA DE RIO BONITO / RBO",
"local_destino": "RODOVIARIA DE SAQUAREMA / SAQ",
"sentido": "volta"
}]
}
const fields = [
`data`,
`dia`,
`inicio`,
`inicio_intervalo`,
`fim_intervalo`,
`fim`,
`jornada`,
`ocorrencia`,
`folga`,
`veiculo`,
`viagens`,
`saida`,
`chegada`,
`local_origem`,
`local_destino`,
`sentido`,
]
const transformObjectUsingThis = ( fields, obj ) => ( key ) =>
( fields.includes( key ) )
? ( obj[ key ] !== null && obj[ key ] !== undefined )
? ( obj[ key ].map )
? obj[ key ].map( transformObjectUsingThis( fields, obj ) )
: Object.assign( {}, { key, value: obj[ key ] } )
: Object.assign( {}, { key, value: obj[ key ] } )
: ( key.constructor === Object )
? Object.keys( key )
.map( transformObjectUsingThis( fields, key ) )
: Object.assign( {}, { key, value: obj[ key ] } )
const createArrayOfObjectsUsingThis = ( fields, obj ) =>
Object.keys( obj ).map( transformObjectUsingThis( fields, obj ) )
const arrayOfObjects = createArrayOfObjectsUsingThis( fields, obj )
console.log('arrayOfObjects: ', arrayOfObjects)
Melhor neh?
const obj = {
"data": "13/04",
"dia": "Qui",
"inicio": "16:24",
"inicio_intervalo": null,
"fim_intervalo": null,
"fim": "20:55",
"jornada": "05:01",
"ocorrencia": null,
"folga": null,
"veiculo": null,
"viagens": [{
"saida": "00:00",
"chegada": "00:00",
"local_origem": "RODOVIARIA DE RIO BONITO / RBO",
"local_destino": "RODOVIARIA DE SAQUAREMA / SAQ",
"sentido": "ida"
}, {
"saida": "00:00",
"chegada": "00:00",
"local_origem": "RODOVIARIA DE RIO BONITO / RBO",
"local_destino": "RODOVIARIA DE SAQUAREMA / SAQ",
"sentido": "volta"
}]
}
const fields = [
`data`,
`dia`,
`inicio`,
`inicio_intervalo`,
`fim_intervalo`,
`fim`,
`jornada`,
`ocorrencia`,
`folga`,
`veiculo`,
`viagens`,
`saida`,
`chegada`,
`local_origem`,
`local_destino`,
`sentido`,
]
const isObject = ( something ) =>
something.constructor === Object
const isNullOrUndefined = ( something ) =>
something !== null && something !== undefined
const createObjectUsing = ( key, obj ) =>
Object.assign( {}, { key, value: obj[ key ] } )
const ifIsArrayThenCreate = ( obj, key, fields ) =>
( obj[ key ].map )
? obj[ key ].map( transformObjectUsingThis( fields, obj ) )
: createObjectUsing( key, obj )
const orIfKeyIsObjectThenCreate = ( obj, key, fields ) =>
( isObject( key ) )
? createArrayFrom( key ).usingThis( fields )
: createObjectUsing( key, obj )
const ifKeyExistsInFieldsList = ( obj, key, fields ) =>
( isNullOrUndefined( obj[ key ] ) )
? ifIsArrayThenCreate( obj, key, fields )
: createObjectUsing( key, obj )
const transformObjectUsingThis = ( fields, obj ) => ( key ) =>
( fields.includes( key ) )
? ifKeyExistsInFieldsList( obj, key, fields )
: orIfKeyIsObjectThenCreate( obj, key, fields )
const createArrayFrom = ( something ) => ({
usingThis: ( fields ) =>
Object.keys( something )
.map( transformObjectUsingThis( fields, something ) )
})
const arrayOfObjects = createArrayFrom( obj ).usingThis( fields )
console.log('arrayOfObjects: ', arrayOfObjects)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
by @suissa