简体   繁体   中英

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'
        }
    }

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!

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

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]
})

As david suggested, you can use a switch statement :

 /* app.get('/gettokens/:chain/:address', async(req, res) => { const address = req.params.address; let chain = splitChain(req.params.chain); } */ function splitChain(value) { let coingecko switch (value) { case 'bsc': coingecko = 'binance-smart-chain' break; case 'eth': coingecko = 'ethereum' break; case 'avax': coingecko = 'avalanche' break; case 'matic': coingecko = 'polygon-pos' break; default: coingecko = null break; } return { moralis: value, coingecko: coingecko } } console.log(splitChain('bsc')) console.log(splitChain('eth')) console.log(splitChain('avax')) console.log(splitChain('wrong-value'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM