Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
How to structure input parameter to object in JS

I have a simple Express API here. When the user submits the chain param I want to convert the parameter to an object. Depending on the input the object will have different values Right now I just have a bunch of if statements stacked

Is there a better practice solution to implement this as this is quite clunky?

app.get('/gettokens/:chain/:address', async(req, res) => {
    const address = req.params.address;
    let chain = req.params.chain;

    if (chain == 'bsc') {
        chain = {
            moralis: 'bsc',
            coingecko: 'binance-smart-chain'
        }
    }

    if (chain == 'eth') {
        chain = {
            moralis: 'eth',
            coingecko: 'ethereum'
        }
    }

    if (chain == 'avax') {
        chain = {
            moralis: 'avalanche',
            coingecko: 'avalanche'
        }
    }

    if (chain == 'matic') {
        chain = {
            moralis: 'matic',
            coingecko: 'polygon-pos'
        }
    }
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can try using a constant object

const chainDef = {
     bsc: {  
                 moralis: ‘bsc’,
                 coingecko: ‘binance-smart-chain’
       } ,
    …
}

// you should validate `req.params.chain` before doing this
const chain = chainDef[req.params.chain]

Hope this helps!

over 4 years ago · Santiago Trujillo Denunciar

0

You can use some configuration object to handle it

like this

 const config = { bsc: { moralis: 'bsc', coingecko: 'binance-smart-chain' }, eth: { moralis: 'eth', coingecko: 'ethereum' }, avax: { moralis: 'avalanche', coingecko: 'avalanche' }, matic: { moralis: 'matic', coingecko: 'polygon-pos' } } const fallback = { moralis: 'something', coingecko: 'something' } const getChain = (key) => config[key] || fallback console.log(getChain('matic')) console.log(getChain('not defined'))

over 4 years ago · Santiago Trujillo Denunciar

0

You could define an object with all your different values and use the expected req.params.chain as keys:

const ChainObjs = {
  bsc: {
    moralis: 'bsc',
    coingecko: 'binance-smart-chain'
  },
  eth: {
    moralis: 'eth',
    coingecko: 'ethereum'
  },
  ...
}

And then you can get the correct value like this

app.get('/gettokens/:chain/:address', async(req, res) => {
  let chain = ChainObjs[req.params.chain]
})
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda