Skip to content

Instantly share code, notes, and snippets.

@ronaiza-cardoso
Last active April 7, 2017 18:42
Show Gist options
  • Save ronaiza-cardoso/58a4deacbb9d24956eba62f1dcd08c4a to your computer and use it in GitHub Desktop.
Save ronaiza-cardoso/58a4deacbb9d24956eba62f1dcd08c4a to your computer and use it in GitHub Desktop.
{
"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 }
})
@ronaiza-cardoso
Copy link
Author

    const fields = [
      `dia`,
      `inicio`,
      `inicio_intervalo`,
      `fim_intervalo`,
      `fim`,
      `jornada`,
      `ocorrencia`,
      `folga`,
      `veiculo`
    ]

    const transformObjectUsingThis = ( fields, obj ) => ( field ) => {
      if ( fields.includes( field ) )
      return Object.assign( {}, { key: field, value: obj[ field ] } )
    }

    const arrayOfObjects = Object.keys( this.detalhesEscalaModal )
      .map( transformObjectUsingThis( fields, this.detalhesEscalaModal ) )

  	console.log('arrayOfObjects: ', arrayOfObjects)

by @suissa

@ronaiza-cardoso
Copy link
Author

  this.detalhesEscalaModal = Object.keys(this.detalhesEscalaModal)
    .reduce((acc, key) => ([...acc, { key, value: this.detalhesEscalaModal[key] }]), [])

  console.log(this.detalhesEscalaModal)

by @vinicius73

@suissa
Copy link

suissa commented Apr 7, 2017

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)

@suissa
Copy link

suissa commented Apr 7, 2017

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