[英]lodash to find common values from array of objects
Find common values from Array of Objects and Transom them 从对象数组中找到常用值并为它们加尾音
tried using lodash groupBy 尝试使用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. 在数据dc中,effDate,expDate是相同的,因此我需要将常见内容保留为平面结构,并将重复项移到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. 我尝试使用lodash uniqBy属性,但没有得到预期的结果。
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. 您需要按
effDate
对项目进行effDate
,然后使用_.pick()
/ _.omit()
, _.map()
并将_.uniqBy()
与rate
作为唯一标识符。
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: 如果您已经导入了整个lodash软件包,则可以使用链接:
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: 和lodash / fp版本:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.