Filtering Keys from an object

My apologies if I have a bit of the lingo wrong; I'll do my best....I'd like to filter out keys and their values based on certain letters within the key. The specific purpose of this is using the Binance node someone made. I'd like to filter out just the items that contain "USD" in the key. For example, in the original object, you'll see:
BTCUSDT : someValue
BTCBNB
ETHUSD
ETHBTC
BNBBUSD
BNBBTC
and so on x100....

I'd like to filter out everything that doesn't have "USD" in it. So in the upper example, out of what's showing there, I'd be left with:
BTCUSDT
ETHUSD
BNBBUSD
and of course their respective values...

Hello,

The following code in a function node should do the job.

// It should be replaced by your msg.payload
let sample_coins = {
    "BTCUSDT":10,
    "BTCBNB":2,
    "ETHUSD":8
}

let filtered_coins = Object.keys(sample_coins)
  .filter(key => key.includes("USD"))
  .reduce((obj, key) => {
    obj[key] = sample_coins[key];
    return obj;
  }, {});

msg.payload = filtered_coins

return msg;

This works perfectly. Thank you very much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.