[英]Match Object keys in array of objects and return key, values that have highest volume value
I have an array of objects, baseAsset key and Volume are part of every object but the volume is different for each object. 我有一个对象数组, baseAsset键和Volume是每个对象的一部分,但每个对象的音量不同。
I want to match the baseAsset key and return the object with the highest volume value. 我想匹配baseAsset键并返回具有最高音量值的对象。 Efficiency and speed is important as array have 3000+ objects 效率和速度很重要,因为阵列有3000多个对象
let tickerA = [{
pair: 'AUDUSD',
baseAsset: 'AUD',
lastPriceUSD: 0.74,
volume: 1000
}, {
pair: 'AUDUSD',
baseAsset: 'AUD',
lastPriceUSD: 0.76,
volume: 2000
}, {
pair: 'USDEUR',
baseAsset: 'USD',
lastPriceUSD: 1.25,
volume: 1200
}, {
pair: 'USDEUR',
baseAsset: 'USD',
lastPriceUSD: 1.19,
volume: 1500
}]
Expected return from a function 期望从函数返回
tickerB = [{
baseAsset: 'AUD',
lastPriceUSD: 0.76,
volume: 2000
}, {
baseAsset: 'USD',
lastPriceUSD: 1.25,
volume: 1500
}]
You can do this in O(n) time by looping over and saving the largest item to an object. 您可以在O(n)时间内通过循环并将最大项目保存到对象来执行此操作。 In the end your values will be in the Object.values
of the groups: 最后,您的值将位于组的Object.values
中:
let tickerA = [{pair: 'AUDUSD',baseAsset: 'AUD',lastPriceUSD: 0.74,volume: 1000}, {pair: 'AUDUSD',baseAsset: 'AUD',lastPriceUSD: 0.76,volume: 2000}, {pair: 'USDEUR',baseAsset: 'USD',lastPriceUSD: 1.25,volume: 1200}, {pair: 'USDEUR',baseAsset: 'USD',lastPriceUSD: 1.19,volume: 1500}] let groups = tickerA.reduce((largest, {baseAsset, lastPriceUSD, volume}) => { /* * if it's a new baseAsset or bigger than a previous one, save it * to the group under the baseAsset key */ if (!largest[baseAsset] || largest[baseAsset]['volume'] < volume ) { largest[baseAsset] = {baseAsset, lastPriceUSD, volume} } return largest }, {}) TickerB = Object.values(groups) console.log(TickerB)
One approach would be to iterate the values of tickerA
and map these to baseAsset
keys if that item's volume
value is greater than that of the current item of that baseAsset
key: 一种方法是迭代tickerA
的值并将这些值映射到baseAsset
键,如果该项的volume
值大于该baseAsset
键的当前项的baseAsset
:
let tickerA = [{ pair: 'AUDUSD', baseAsset: 'AUD', lastPriceUSD: 0.74, volume: 1000 }, { pair: 'AUDUSD', baseAsset: 'AUD', lastPriceUSD: 0.76, volume: 2000 }, { pair: 'USDEUR', baseAsset: 'USD', lastPriceUSD: 1.25, volume: 1200 }, { pair: 'USDEUR', baseAsset: 'USD', lastPriceUSD: 1.19, volume: 1500 }]; /* Use map to relate baseAsset key of tickerA item with current max volume value */ const map = new Map() /* Iterate tickerA items, searching for greatest volume value per baseAsset class */ for(const item of tickerA) { const assetMatch = map.get(item.baseAsset); if(assetMatch && item.volume < assetMatch.volume) { /* If matching item (by asset found) with volume greater than that of current tickerA item, then disregard the current item */ continue; } else { /* Otherwise, this tickerA item is; the first of the asset class, or greater in volume so we'll update the map entry for this asset class */ map.set(item.baseAsset, item); } } /* Extract the map values as an array */ const tickerB = Array.from(map.values()); console.log(tickerB);
This alternative groups baseAsset
and at the end extracts the grouped values. 此替代组将baseAsset
分组,最后提取分组值。
This is a O(n)
time complexity . 这是O(n)
时间复杂度 。
let tickerA = [{pair: 'AUDUSD',baseAsset: 'AUD',lastPriceUSD: 0.74,volume: 1000}, {pair: 'AUDUSD',baseAsset: 'AUD',lastPriceUSD: 0.76,volume: 2000}, {pair: 'USDEUR',baseAsset: 'USD',lastPriceUSD: 1.25,volume: 1200}, {pair: 'USDEUR',baseAsset: 'USD',lastPriceUSD: 1.19,volume: 1500}]; let result = Object.values(tickerA.reduce((a, {baseAsset, lastPriceUSD, volume}) => { let {volume: current} = a[baseAsset] || {volume: Number.MAX_SAFE_INTEGER}; if (current < volume) a[baseAsset].volume = volume; else a[baseAsset] = {baseAsset, lastPriceUSD, volume}; return a; }, Object.create(null))); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Use reduce
and Object.values
: 使用reduce
和Object.values
:
let tickerA = [{pair: 'AUDUSD',baseAsset: 'AUD',lastPriceUSD: 0.74,volume: 1000}, {pair: 'AUDUSD',baseAsset: 'AUD',lastPriceUSD: 0.76,volume: 2000}, {pair: 'USDEUR',baseAsset: 'USD',lastPriceUSD: 1.25,volume: 1200}, {pair: 'USDEUR',baseAsset: 'USD',lastPriceUSD: 1.19,volume: 1500}]; const res = Object.values(tickerA.reduce((acc, { baseAsset, lastPriceUSD, volume }) => { acc[baseAsset] = (!acc[baseAsset] || acc[baseAsset].volume < volume) ? { baseAsset, lastPriceUSD, volume } : acc[baseAsset]; return acc; }, {})); console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.