簡體   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