Lodash从对象数组中查找公用值

时间:2019-02-05 20:55:24

标签: javascript ecmascript-6 lodash

从对象数组中找到通用值并为它们加注

使用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
      }
    ]
  }
]

数据dc,effDate,expDate中的数据相同,因此我需要将常见内容保留为扁平结构,并将重复项移到rateCharge中。

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

我尝试使用lodash uniqBy属性,但没有得到预期的结果。

1 个答案:

答案 0 :(得分:2)

您需要按effDate对项目进行分组,然后使用_.pick() / _.omit()_.map()和{{1 }},其中_.uniqBy()是唯一标识符。

Typescript example(打开浏览器控制台)

rate
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)

如果您已经导入了整个lodash软件包,则可以使用链接:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
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)

还有lodash / fp版本:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
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)