简体   繁体   English

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

[英]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 : 使用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.

相关问题 从对象数组中返回键/值对最高的对象 - Return object with highest key/value pairs from an array of objects 如何比较对象数组并返回具有匹配和不匹配键值的 object 数组 - How to compare array of objects and return an array of object with match and non match key value 如何从具有最高键值和名称的数组中返回对象? - how to return object from array with highest key values along with name? 从对象数组中匹配键和值并返回仅具有该键的新 object 的正确方法是什么 - What is the proper way to match key and value from an array of objects and return a new object with only that key 收集数组中键匹配映射键值的对象 - collect objects in array who's keys match a map key, value 从数组中的键向对象数组 object 添加键和值 - Add key and value to array of objects object from the keys in array 对象列表:返回属性值最高的对象 - List of objects: return object with highest attribute value JSON object 返回包含键及其值的对象数组 - JSON object return of an array of objects which contains the keys and its values 如何使用 Joi 验证嵌套 object 的键应与外部对象匹配的另一个键的值为数组的键? - How to validate nested object whose keys should match with outer objects another key whose value is array using Joi? 按最高值(包括最高匹配值)过滤对象数组 - Filter an array of objects by a highest value (including highest matching values)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM