简体   繁体   中英

lodash to find common values from array of objects

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.

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM