I have the following array:
[
{
baseAsset: 'XRP',
quoteAsset: 'ETH',
quoteAssetPrecision: 6,
baseAssetPrecision: 8,
price: '0.00025170'
},
{
baseAsset: 'MOD',
quoteAsset: 'BTC',
quoteAssetPrecision: 8,
baseAssetPrecision: 4,
price: '0.00004280'
},
{
baseAsset: 'MOD',
quoteAsset: 'ETH',
quoteAssetPrecision: 8,
baseAssetPrecision: 4,
price: '0.00116700'
},
{
baseAsset: 'ENJ',
quoteAsset: 'BTC',
quoteAssetPrecision: 8,
baseAssetPrecision: 8,
price: '0.00004508'
},
{
baseAsset: 'ENJ',
quoteAsset: 'ETH',
quoteAssetPrecision: 6,
baseAssetPrecision: 8,
price: '0.00064370'
},
{
baseAsset: 'STORJ',
quoteAsset: 'BTC',
quoteAssetPrecision: 8,
baseAssetPrecision: 8,
price: '0.00002090'
},
{
baseAsset: 'STORJ',
quoteAsset: 'ETH',
quoteAssetPrecision: 6,
baseAssetPrecision: 8,
underlying: 'STORJETH',
symbol: 'STORJETH',
price: '0.00029910'
}
]
What I want to get:
[
{
ticker: 'XRP',
precision: 8,
},
{
ticker: 'MOD',
precision: 4,
},
{
ticker: 'BTC',
precision: 8,
},
{
ticker: 'ENJ',
precision: 8,
},
{
ticker: 'STORJ',
precision: 8,
},
{
ticker: 'ETH',
precision: 6,
}
]
As you can see, each precision
prop is associated with an asset and isn't changed. I want to extract each currency and its precision
from my array of currency pairs. What an elegant solution can be provided via lodash
? Other options are accepted. I was trying to implement this using native javascript, but solution didn't satisfied me:
const currenciesFetched = tradingPairsWithPrices
.flatMap((pair) => [
{
ticker: pair.baseAsset,
precision: pair.baseAssetPrecision,
},
{
ticker: pair.quoteAsset,
precision: pair.quoteAssetPrecision,
},
])
.filter((v, i, a) => {
return a.findIndex((t) => t.ticker === v.ticker) === i
})
Any ideas?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment