Editing A Json In Js And Putting It In An Array
so I have the following json. { 'BTC': { 'available': 0.00024868, 'onOrder': 0, 'btcValue': 0.00024868, 'btcTotal': 0.00024868 }, 'LTC': { 'available': 0, '
Solution 1:
Something like this might work:
const json = `{"BTC":{"available":0.00024868,"onOrder":0,"btcValue":0.00024868,"btcTotal":0.00024868},"LTC":{"available":0,"onOrder":0,"btcValue":0,"btcTotal":0},"ETH":{"available":0,"onOrder":0,"btcValue":0,"btcTotal":0},"NEO":{"available":0,"onOrder":0,"btcValue":0,"btcTotal":0},"BNB":{"available":0.08943066,"onOrder":0,"btcValue":0.0004663808919,"btcTotal":0.0004663808919}}`;
constdata = JSON.parse(json);
const processed = Object.entries(data)
.filter(([, { available }]) => available > 0)
.map(([asset, { available, btcValue }]) => {
return { asset, available, btcValue };
});
const asArray = processed.map(Object.values);
console.table(processed);
console.log(asArray);
Object.entries
returns an array of key-value pairs. Since it's an array, you can:
- call
filter
method to only keep items whereavailable
was greater than 0 - call
map
method to transform the filtered array of key-value pairs into an array of objects (where each object has properties:asset
,available
,btcValue
)
You can get rid of asArray
if you want, if it's not useful. It's just to give you an idea of what's possible.
Post a Comment for "Editing A Json In Js And Putting It In An Array"