避免lodash导入整个库,并避免_导致整个库加载

时间:2019-02-08 10:50:52

标签: javascript ecmascript-6 lodash

我需要从lodash库中导入_,这将从lodash中导入所有类。是否有任何解决方案可以通过仅导入所需的库来避免导入此“ _”。

import _ from 'lodash';


import './style.css';
import _ from 'lodash';
import
{pick,head,uniqBy,map,partialRight as pr,omit} from 'lodash';

const baseProps = ['dc', 'effDate', 'expDate'];

const data = [{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":1,"minCharge":2},{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":2,"minCharge":6},{"dc":1,"effDate":"1/2/2019","expDate":"1/2/2019","rate":4,"minCharge":7}];

const result =_(data).groupBy('effDate')
  .map(g => ({
    ...pick(head(g), baseProps),
    rateCharge: uniqBy(map(g, pr(omit, baseProps)), 'rate')  
  })).value();

console.log(result)

Actual & Expected Output
[
  {
    "dc": 1,
    "effDate": "1/2/2019",
    "expDate": "1/2/2019",
    "rateCharge": [
      {
        "rate": 1,
        "minCharge": 2
      },
      {
        "rate": 2,
        "minCharge": 6
      },
      {
        "rate": 4,
        "minCharge": 7
      }
    ]
  }
];

我的组织编码标准不允许从“ loadash”导入“ _”,因为它将导入整个库。由于我在Typescritp Angular中工作,因此无法找到替代解决方案。

1 个答案:

答案 0 :(得分:-3)

只需删除

import _ from 'lodash';

并更改此行:

import {pick,head,uniqBy,map,partialRight as pr,omit} from 'lodash';

对此:

import {pick,head,uniqBy,map,partialRight as pr,omit, groupBy} from 'lodash';

也更改此行:

const result =_(data).groupBy('effDate’)

对此:

const result =data.groupBy('effDate’)

应该可以。