0

i have been facing coding issue while trying to reduce the data stream. Below is the sample code

Data is being filtered on the base of exchanges, now i need to filter out the pairs not given in pair array.

//data stream
var dataStream = {
  "Coinbase": { "pairs": { "ETH": ["USD", "GBP", "BTC"], "DAI": ["USDC"], "LTC": ["GBP", "BTC", "USD", "EUR"], "ETC": ["EUR", "BTC", "USD"] } },
  "Binance": { "pairs": { "ETH": ["USD", "BTC"], "DAI": ["USDC"], "LTC": ["GBP", "BTC"], "ETC": ["EUR"] } },
  "CoinCorner": { "pairs": { "BTC": ["GBP", "EUR","LTC"] } }
};
//allowed exchages
    var exchanges = ["Coinbase", "Binance", "Bitstamp"];
//allowed pairs
var pair = ["BTC","ETH","LTC"];

const filtered = Object.keys(dataStream)
  .filter(key => exchanges.includes(key))
  .reduce((obj, key) => {
    obj[key] = dataStream[key].pairs;//do some reduction
    return obj;
  }, {});

console.log("data filtered:" + JSON.stringify(filtered));

I needed to reduce the result on the base of 'pair' array where including pairs only get to the object.

Update Result must be

var dataStream = {
        "Coinbase": { "pairs": { "ETH": ["USD", "GBP", "BTC"], "LTC": ["GBP", "BTC", "USD", "EUR"] } },
          "Binance": { "pairs": { "ETH": ["USD", "BTC"], "LTC": ["GBP", "BTC"]} }
       };

If someone can help or give clue, i will be thankful. Thanks for your time.

3
  • 3
    Post what you're expecting the output to be. Commented Feb 13, 2019 at 5:45
  • @EvanTrimboli thank for the comment, sorry question updated. Commented Feb 13, 2019 at 5:59
  • 1
    @SuhailMumtazAwan - It seems your updated answer has been updated many a times. Can you please have one version of the requirement? Commented Feb 13, 2019 at 6:28

2 Answers 2

2

You can try following using Object.entries, Array.includes and Array.reduce

var dataStream = {"Coinbase": { "pairs": { "ETH": ["USD", "GBP", "BTC"], "DAI": ["USDC"], "LTC": ["GBP", "BTC", "USD", "EUR"], "ETC": ["EUR", "BTC", "USD"] } },"Binance": { "pairs": { "ETH": ["USD", "BTC"], "DAI": ["USDC"], "LTC": ["GBP", "BTC"], "ETC": ["EUR"] } },"CoinCorner": { "pairs": { "BTC": ["GBP", "EUR"] } }};

var exchanges = ["Coinbase", "Binance", "Bitstamp"];
var pair = ["BTC","ETH","LTC"];

let result = Object.entries(dataStream).reduce((a,[k,v]) => {
  if(exchanges.includes(k)) {
    a[k] = Object.entries(v.pairs).reduce((ac,[kc,vc]) => {
      if(pair.includes(kc)) ac[kc] = vc;
      return ac;
    },{});
  }
  return a;
}, {});
console.log(result);

Or you can simply use a for loop with Array.includes

var dataStream = {"Coinbase": { "pairs": { "ETH": ["USD", "GBP", "BTC"], "DAI": ["USDC"], "LTC": ["GBP", "BTC", "USD", "EUR"], "ETC": ["EUR", "BTC", "USD"] } },"Binance": { "pairs": { "ETH": ["USD", "BTC"], "DAI": ["USDC"], "LTC": ["GBP", "BTC"], "ETC": ["EUR"] } },"CoinCorner": { "pairs": { "BTC": ["GBP", "EUR"] } }};

var exchanges = ["Coinbase", "Binance", "Bitstamp"];
var pair = ["BTC","ETH","LTC"];

let result = {};

for (let exchange in dataStream) {
  if(exchanges.includes(exchange)) {
      result[exchange] = {};
      for (let p in dataStream[exchange].pairs) {
        if(pair.includes(p)) result[exchange][p] = dataStream[exchange].pairs[p];
      }
  }
}

console.log(result);

Edit

Based on updated result required, you can try following. Please note, converting pair array into object helps in improving performance as it avoids using includes operation every time to search.

var dataStream = {"Coinbase": { "pairs": { "ETH": ["USD", "GBP", "BTC"], "DAI": ["USDC"], "LTC": ["GBP", "BTC", "USD", "EUR"], "ETC": ["EUR", "BTC", "USD"] } },
  "Binance": { "pairs": { "ETH": ["USD", "BTC"], "DAI": ["USDC"], "LTC": ["GBP", "BTC"], "ETC": ["EUR"] } },
  "CoinCorner": { "pairs": { "BTC": ["GBP", "EUR","LTC"] } }};

var exchanges = ["Coinbase", "Binance", "Bitstamp"];
var pair = ["BTC","ETH","LTC"];
let pairObj = pair.reduce((a,c) => Object.assign(a, {[c] : c}), {});

for (let d in dataStream) {
  if(exchanges.includes(d)) {
    let pairs = dataStream[d].pairs;
    for(let p in pairs) {
      // if the pair exists in pairObj proceed to check in its value, else remove from object 
      if(pairObj[p]) { 
        // filter the values based on entries in pairObj
        let r = pairs[p].filter(v => pairObj[v]);
        // If there was existing value, update the object else remove it from object
        if(r.length) pairs[p] = r;
        else delete pairs[p];
      } else delete pairs[p];
    }
  } else delete dataStream[d];
}
console.log(dataStream);

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, can you please see the expected result, question updated.
sorry, it was my mistake. your answer is correct. Thanks for the value able help.
@SuhailMumtazAwan - Glad to help you
2

This seems to be working, I cloned original data, just to be sure that I don't mutate the original data

var dataStream = {
  "Coinbase": { "pairs": { "ETH": ["USD", "GBP", "BTC"], "DAI": ["USDC"], "LTC": ["GBP", "BTC", "USD", "EUR"], "ETC": ["EUR", "BTC", "USD"] } },
  "Binance": { "pairs": { "ETH": ["USD", "BTC"], "DAI": ["USDC"], "LTC": ["GBP", "BTC"], "ETC": ["EUR"] } },
  "CoinCorner": { "pairs": { "BTC": ["GBP", "EUR"] } }
};
//allowed exchages
var exchanges = ["Coinbase", "Binance", "Bitstamp"];
//allowed pairs
var allowedPairs = ["BTC","ETH","LTC"];

var clonedStream = JSON.parse(JSON.stringify(dataStream))

const filtered = Object.keys(clonedStream)
  .filter(key => exchanges.includes(key))
  .reduce((obj, key) => {
    var filteredPairs = {pairs: {}};

    for(let p of allowedPairs) {
      filteredPairs['pairs'][p] = dataStream[key].pairs[p] || []
    }

    obj[key] = filteredPairs;
    return obj;
  }, {});

console.log(JSON.stringify(filtered));

The result is

{
   "Coinbase":{
      "pairs":{
         "BTC":[

         ],
         "ETH":[
            "USD",
            "GBP",
            "BTC"
         ],
         "LTC":[
            "GBP",
            "BTC",
            "USD",
            "EUR"
         ]
      }
   },
   "Binance":{
      "pairs":{
         "BTC":[

         ],
         "ETH":[
            "USD",
            "BTC"
         ],
         "LTC":[
            "GBP",
            "BTC"
         ]
      }
   }
}

2 Comments

thanks for the answer, i am testing it with large data sets.
@SuhailMumtazAwan how will answer effect large data sets?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.