我在尝试减少数据流时遇到了编码问题。下面是示例代码
正在基于交换过滤数据,现在我需要过滤掉未在结对数组中给出的对。
//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));
我需要在“ pair”数组的基础上减少结果,其中包括pairs只能到达对象。
更新 结果必须为
var dataStream = {
"Coinbase": { "pairs": { "ETH": ["USD", "GBP", "BTC"], "LTC": ["GBP", "BTC", "USD", "EUR"] } },
"Binance": { "pairs": { "ETH": ["USD", "BTC"], "LTC": ["GBP", "BTC"]} }
};
如果有人可以提供帮助或提供线索,我将很感激。感谢您的宝贵时间。
答案 0 :(得分:2)
这似乎可行,我克隆了原始数据,只是为了确保我不对原始数据进行突变
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));
结果是
{
"Coinbase":{
"pairs":{
"BTC":[
],
"ETH":[
"USD",
"GBP",
"BTC"
],
"LTC":[
"GBP",
"BTC",
"USD",
"EUR"
]
}
},
"Binance":{
"pairs":{
"BTC":[
],
"ETH":[
"USD",
"BTC"
],
"LTC":[
"GBP",
"BTC"
]
}
}
}
答案 1 :(得分:2)
您可以尝试使用Object.entries,Array.includes和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);
或者您也可以在 Array.includes
中使用for循环
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);
修改
根据所需的更新结果,您可以尝试以下操作。请注意,将对数组转换为对象有助于提高性能,因为它避免了每次搜索都使用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","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);