Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lodash - _.some() with condition/counter

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() ?

like image 235
eol Avatar asked Dec 06 '16 07:12

eol


2 Answers

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>
like image 168
Ori Drori Avatar answered Oct 13 '22 22:10

Ori Drori


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>
like image 20
fafl Avatar answered Oct 13 '22 23:10

fafl