1

Find common values from Array of Objects and Transom them

tried using lodash groupBy

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

var expectedResult=[
  {
    "dc": 1,
    "effDate": "1/2/2019",
    "expDate": "1/2/2019",
    "rateCharge": [
      {
        "rate": 1,
        "minCharge": 2
      },
      {
        "rate": 2,
        "minCharge": 6
      },
      {
        "rate": 4,
        "minCharge": 7
      }
    ]
  }
]

in data dc,effDate,expDate are same, so i need to keep common things as flat structure and move repeating items into rateCharge.

var expectedResult=uniqBy(data,(val1.rate,val2.rate) => {
  val1.rate!=val2.rate;    
});

I have tried using lodash uniqBy property but i am not getting expected result.

1 Answer 1

2

You need to group the items by the effDate, and then map the groups to the required form using _.pick() / _.omit() , _.map(), and using _.uniqBy() with the rate as the unique identifier.

Typescript example (open the browsers console)

const { flow, partialRight: pr, groupBy, map, head, pick, omit, uniqBy } = _

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

const fn = flow(
  pr(groupBy, EFF_DATA),
  pr(map, g => ({
    ...pick(head(g), baseProps),
    rateCharge: uniqBy(map(g, pr(omit, baseProps)), 'rate')
  }))
)

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 = fn(data)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

If you already import the entire lodash packages, you can use chaining:

const { flow, partialRight: pr, groupBy, map, head, pick, omit, uniqBy } = _

const EFF_DATA = 'effDate'
const baseProps = ['dc', EFF_DATA, '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(EFF_DATA)
  .map(g => ({
    ...pick(head(g), baseProps),
    rateCharge: uniqBy(map(g, pr(omit, baseProps)), 'rate')  
  }));

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

And the lodash/fp version:

const { flow, groupBy, map, head, pick, omit, uniqBy, assoc } = _;

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

const fn = flow(
  groupBy(EFF_DATA),
  map(g => assoc(
    'rateCharge',
    flow(map(omit(baseProps)), uniqBy('rate'))(g),
    pick(baseProps, head(g))
  ))
)

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 = fn(data)

console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

Sign up to request clarification or add additional context in comments.

15 Comments

lodash/fp exists so you don't have to pr everything
Unfortunately, most users don't use lodash/fp yet. I usually add a lodash/fp solution as well, which I'll do soon.
Nice update. Maybe mention how this works because you're stringing all of the values together with - to make a grouping key. And maybe mention how using - will cause an odd behaviour if the values may possibly contain -. Maybe too much to mention how a custom data type could support real compound data equality :D
What is underscore means? How can i use the same using lodash with angular(typescript)? Any difference in syntax? Please help
Instead of destructuring the underscore, in angular you will import the various functions from lodash (or lodash/fp). That's the main difference in syntax.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.