繁体   English   中英

匹配对象数组中的对象键并返回具有最高音量值的值

[英]Match Object keys in array of objects and return key, values that have highest volume value

我有一个对象数组, baseAsset键和Volume是每个对象的一部分,但每个对象的音量不同。

我想匹配baseAsset键并返回具有最高音量值的对象。 效率和速度很重要,因为阵列有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
}]

期望从函数返回

tickerB = [{
    baseAsset: 'AUD',
    lastPriceUSD: 0.76,
    volume: 2000
}, {
    baseAsset: 'USD',
    lastPriceUSD: 1.25,
    volume: 1500
}]

您可以在O(n)时间内通过循环并将最大项目保存到对象来执行此操作。 最后,您的值将位于组的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) 

一种方法是迭代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); 

此替代组将baseAsset分组,最后提取分组值。

这是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; } 

使用reduceObject.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM