我有一组对象, baseAsset键和“体积”是每个对象的一部分 但是每个对象的音量都不同。
我想匹配baseAsset键并返回带有 最高音量值。 效率和速度很重要,因为数组有3000多个对象
$ curl http://my.site.net/ -v -I --http1.0 --Header 'Host:'
* Trying 100.101.102.103...
* TCP_NODELAY set
* Connected to my.site.net (100.101.102.103) port 80 (#0)
> HEAD / HTTP/1.0
> User-Agent: curl/7.63.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: awselb/2.0
< Date: Fri, 10 May 2019 23:46:54 GMT
< Content-Type: text/html
< Content-Length: 150
< Connection: close
< Location: https://internal-mysite-1234567890.us-west-2.elb.amazonaws.com:443/
函数的预期收益
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
}]
答案 0 :(得分:1)
您可以通过循环并将最大的项目保存到对象来在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)
答案 1 :(得分:1)
一种方法是迭代tickerA
的值,并将其映射到baseAsset
键,如果该项的volume
值大于该{{1}的当前项的值}键:
baseAsset
答案 2 :(得分:0)
此替代组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; }
答案 3 :(得分:0)
使用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; }