1

I need to import _ from lodash library which will import all classes from lodash. Is there any solution to avoid importing this '_', by only importing the required libraries.

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
      }
    ]
  }
];

My organization coding standard won't allow importing '_" from 'loadash', because it will import the entire library. As I am working in Typescritp Angular I am not able to find alternate solution.

Mithun S
  • 408
  • 8
  • 20
  • This is not working? import {pick,...} from 'lodash'; – jo_va Feb 08 '19 at 11:04
  • 1
    Possible duplicate of [How to Import a Single Lodash Function?](https://stackoverflow.com/questions/43479464/how-to-import-a-single-lodash-function) – l2aelba Feb 08 '19 at 11:10

1 Answers1

-2

Just remove

import _ from 'lodash';

and change this line:

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

to this:

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

also change this line:

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

To this:

const result =data.groupBy('effDate’)

it should work.