lodash - 带有条件/计数器的_.some()

eol*_*eol 5 javascript lodash

请考虑以下任务:

我们列出了不同欧洲城镇的日平均气温.

{ 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] }
Run Code Online (Sandbox Code Playgroud)

我们想把这些城镇分为两组:"温暖"和"热"."温暖"应该是至少有3天温度大于19的城镇."炎热"应该是每天温度高于19的城镇.

我最终做的是:

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
Run Code Online (Sandbox Code Playgroud)

是否有更优雅的方法来检查"温度> 19"的"至少3天"而不是使用该_.sumBy功能?也许用_.some()

Ori*_*ori 4

我提供了一个普通的 js 解决方案和一个 lodash 解决方案。

香草JS

您可以使用过滤器计算符合条件的天数:

tempaturesArray.filter((t) => t > 19).length
Run Code Online (Sandbox Code Playgroud)

您可以使用 vanilla JS 来完成此操作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: []});
Run Code Online (Sandbox Code Playgroud)

tempaturesArray.filter((t) => t > 19).length
Run Code Online (Sandbox Code Playgroud)


洛达什

用于_.mapValues()将数组转换为冷/暖/热字符串,然后用于_.invertBy()将值切换为键,并收集数组中的国家/地区名称。我曾经_.sumBy()计算过天数,但删除了_.every(),所以一次就可以计算两种气候:

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();
Run Code Online (Sandbox Code Playgroud)

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: []});
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)