如何在loadash中找到min和max

时间:2017-12-08 11:42:17

标签: javascript arrays

{
  "item_name": "Chicken Tangy Combo",
  "item_key": "mezvts3qv1rl56vj",
  "item_price": 22,
  "quantity": 1,
  "ingredients": [
    {
      "group_key": "9lleke6mc60ulon1",
      "key": "qjuejdqhhcyz0orm",
      "ingredient_name": "2 Bread",
      "price": 1,
      "qun": 1,
      "max": 1,
      "min": 0
    },
    {
      "group_key": "qctfdwlc54v8xdet",
      "key": "rkgqnzkpktjfkfwz",
      "ingredient_name": "Onion Rings",
      "price": 3,
      "qun": 1,
      "max": 2,
      "min": 1
    }
  ]
}

如何使用loadash

查找最大值和最小值

我使用了_(this.item.ingredients).groupBy('group_key').map((objs, key) => ({ 'group_key': key, 'total': _.sumBy(objs, 'qun'), 'max':_.maxBy(objs, 'max')})).value();

groupBy和sumBy工作正常但无法获得最大值而不是我获得整个对象

1 个答案:

答案 0 :(得分:0)

我对Loadash一无所知,在搜索时,我找到了Lodash并在菜单中选择了_.max.

https://lodash.com/docs#maxhttps://lodash.com/docs#maxBy

也许这样:_(this.item.ingredients).groupBy('group_key').map((objs, key) => ({ 'group_key': key, 'total': _.sumBy(objs, 'qun'), 'max':_.max(objs, 'max')})).value();