Consider the following task:
We have a list of the daily average temperatures of different European towns.
{ Hamburg: [14, 15, 16, 14, 18, 17, 20, 11, 21, 18, 19,11 ],
Munich: [16, 17, 19, 20, 21, 23, 22, 21, 20, 19, 24, 23],
Madrid: [24, 23, 20, 24, 24, 23, 21, 22, 24, 20, 24, 22],
Stockholm: [16, 14, 12, 15, 13, 14, 14, 12, 11, 14, 15, 14],
Warsaw: [17, 15, 16, 18, 20, 20, 21, 18, 19, 18, 17, 20] }
We want to sort these towns into two groups: "warm" and "hot". "warm" should be towns that have at least 3 days with a temperature greater than 19. "hot" should be towns where the temperature is greater than 19 every day.
What I ended up doing is:
const _ = require('lodash');
let cities = {
Hamburg: [14, 15, 16, 14, 18, 17, 20, 11, 21, 18, 19,11 ],
Munich: [16, 17, 19, 20, 21, 23, 22, 21, 20, 19, 24, 23],
Madrid: [24, 23, 20, 24, 24, 23, 21, 22, 24, 20, 24, 22],
Stockholm: [16, 14, 12, 15, 13, 14, 14, 12, 11, 14, 15, 14],
Warsaw: [17, 15, 16, 18, 20, 20, 21, 18, 19, 18, 17, 20]
};
let isHot = (degrees) => {
return degrees > 19;
};
function getMinCategories(cities) {
let res = {
hot: [],
warm: []
};
_.forEach(cities, function(val,key) {
if(_.every(val, isHot)){
res.hot.push(key);
} else if(_.sumBy(val, degree => isHot(degree) ? 1 : 0) > 2){
res.warm.push(key);
}
});
return res;
}
console.log(getMinCategories(cities)); // prints { hot: [ 'Madrid' ], warm: [ 'Munich', 'Warsaw' ] } which is correct
Is there a more elegant way to check for the "at least 3 days with temperature > 19" instead of using the _.sumBy
function? Maybe using _.some()
?
I've included a vanilla js solution, and a lodash one.
Vanilla JS
You can calculate the amount of days that match the criteria using filter:
tempaturesArray.filter((t) => t > 19).length
You can do it with vanilla JS using Array#reduce
:
const result = Object.keys(cities).reduce(( obj, city ) => {
const days = cities[city].filter((t) => t > 19).length;
const climate = days === cities[city].length ? 'hot' : (days >= 3 ? 'warm' : null);
climate && obj[climate].push(city);
return obj;
}, { hot: [], warm: []});
const cities = {
Hamburg: [14, 15, 16, 14, 18, 17, 20, 11, 21, 18, 19,11 ],
Munich: [16, 17, 19, 20, 21, 23, 22, 21, 20, 19, 24, 23],
Madrid: [24, 23, 20, 24, 24, 23, 21, 22, 24, 20, 24, 22],
Stockholm: [16, 14, 12, 15, 13, 14, 14, 12, 11, 14, 15, 14],
Warsaw: [17, 15, 16, 18, 20, 20, 21, 18, 19, 18, 17, 20]
};
const result = Object.keys(cities).reduce(( obj, city ) => {
const days = cities[city].filter((t) => t > 19).length;
const climate = days === cities[city].length ? 'hot' : (days >= 3 ? 'warm' : null);
climate && obj[climate].push(city);
return obj;
}, { hot: [], warm: []});
console.log(result);
lodash
Use _.mapValues()
to convert the arrays into a cold/warm/hot string, and then use _.invertBy()
to switch the values to the keys, and collect the names of the countries in an array. I've used _.sumBy()
to caluculate the days, but removed the _.every()
, so one pass will calculate both climates:
const result = _(cities)
.mapValues((temperature, country) => {
const days = _.sumBy(temperature, (t) => t > 19);
return days === temperature.length ? 'hot' : (days >= 3 ? 'warm' : 'cold');
})
.invertBy()
.pick(['warm', 'hot'])
.value();
const cities = {
Hamburg: [14, 15, 16, 14, 18, 17, 20, 11, 21, 18, 19,11 ],
Munich: [16, 17, 19, 20, 21, 23, 22, 21, 20, 19, 24, 23],
Madrid: [24, 23, 20, 24, 24, 23, 21, 22, 24, 20, 24, 22],
Stockholm: [16, 14, 12, 15, 13, 14, 14, 12, 11, 14, 15, 14],
Warsaw: [17, 15, 16, 18, 20, 20, 21, 18, 19, 18, 17, 20]
};
const result = _(cities)
.mapValues((temperature, country) => {
const days = _.sumBy(temperature, (t) => t > 19);
return days === temperature.length ? 'hot' : (days >= 3 ? 'warm' : 'cold');
})
.invertBy()
.pick(['warm', 'hot'])
.value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
You can use pickBy like this:
var cities = {
"Hamburg": [14,15,16,14,18,17,20,11,21,18,19,11],
"Munich": [16,17,19,20,21,23,22,21,20,19,24,23],
"Madrid": [24,23,20,24,24,23,21,22,24,20,24,22],
"Stockholm": [16,14,12,15,13,14,14,12,11,14,15,14],
"Warsaw": [17,15,16,18,20,20,21,18,19,18,17,20]
}
var hotCities = _(cities)
.pickBy(temps => _(temps).map(x => x > 19).every())
.value();
var warmCities = _(cities)
.pickBy((temps, name) => hotCities[name] === undefined) // already hot
.pickBy(temps => _(temps).filter(x => x > 19).size() >= 3)
.value();
console.log(hotCities)
console.log(warmCities)
<script src="https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With