Skip to content
Advertisement

editing a JSON in JS and putting it in an array

so I have the following json.

JavaScript

I need to remove the items that don’t have a value in the “available” field (such as NEO and ETH and set the result in an array. Then remove the onOrder and btcTotal fields.

such as:

BTC 0.00024868 0.00024868

BNB 0.8943066 0.0004663808919

I am writing my little project in JS on NodeJS as a little hobby project. But, so far all I am able to get right is listing the JSON in the console.

Advertisement

Answer

Something like this might work:

JavaScript

Object.entries returns an array of key-value pairs. Since it’s an array, you can:

  • call filter method to only keep items where available 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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement