Get max price of most common prices
Lodash. Turn {price: repeats} set into array of objects ([{price: repeats}, {price:repeats}]). Then pick the most common price. Filter out other prices. Pick max price among most common prices (if there is more than one).
by Yurii Predborskyi
February 27, 2018
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
<div id='wrapper'>
<div id='container'>
</div>
</div>
CSS
#wrapper {
font-family: monospace;
background-color: white;
}
JavaScript
function say(text) {
text = typeof text === typeof {} ? JSON.stringify(text) : text;
document.getElementById('container').innerHTML += text + '<br>';
}
let items = {
100: 50,
175: 50,
150: 50,
300: 10,
50: 60,
325: 60,
275: 60
};
say('items are:');
say(items);
let prices = _.keys(items).map(key => { return { price: key, repeats: items[key] }; });
let maxRepeats = _.maxBy(prices, x => x.repeats).repeats;
let result = +_.maxBy(prices.filter(x => { return x.repeats >= maxRepeats }), x => +x.price).price;
say('expected: 325')
say(result);