我有两个不同的对象数组,它们是从聚合函数中获取的,因为我使用的是两个不同的集合。
我已尝试使用此处介绍的map函数,但是它不能解决我的问题,我还能做些什么才能获得所需的结果?
qrySearch = [{
sName: 'SomePlace1',
lBusinessID: 37343,
SystemID: 5000152
},
{
sName: 'SomePlace2',
lBusinessID: 39780,
SystemID: 5000156
},
{
sName: 'SomePlace3',
lBusinessID: 50772,
SystemID: 5000519
},
{
sName: 'SomePlace4',
lBusinessID: 31079,
SystemID: 5000384
}
]
和
qrySearchLocID = [{
LocalLabID: '123f',
_ID: 'SomePlace1',
AppLabID: 3,
count: 15
},
{
LocalLabID: '12BC',
_ID: 'SomePlace2',
AppLabID: 3,
count: 40
}
];
尝试结果后仅是此数组:
qrySearch = [{
sName: 'SomePlace1',
lBusinessID: 37343,
SystemID: 5000152
},
{
sName: 'SomePlace2',
lBusinessID: 39780,
SystemID: 5000156
},
{
sName: 'SomePlace3',
lBusinessID: 50772,
SystemID: 5000519
},
{
sName: 'SomePlace4',
lBusinessID: 31079,
SystemID: 5000384
},
]
为简单起见,使数组简短。我需要比较_ID(如果它与sName相匹配)以获得所需的输出:
result = [{
sName: 'SomePlace1',
lBusinessID: 37343,
SystemID: 5000152,
LocalLabID: '123f',
AppLabID: 3,
count: 15
},
{
sName: 'SomePlace2',
lBusinessID: 39780,
SystemID: 5000156,
LocalLabID: '12BC',
AppLabID: 3,
count: 40
},
{
sName: 'SomePlace3',
lBusinessID: 50772,
SystemID: 5000519
},
{
sName: 'SomePlace4',
lBusinessID: 31079,
SystemID: 5000384
}
]
我尝试使用以下示例:
var result = qrySearch.map((e, _) =>
(_ = qrySearchLocID.find((q) => q._ID=== e.sName)) ?
{ ...e, ...{ _ID: _._ID} } : e);
,但结果缺少计数。
答案 0 :(得分:0)
您也可以扩展占位符_
来合并值
const qrySearch = [{
sName: 'SomePlace1',
lBusinessID: 37343,
SystemID: 5000152
},
{
sName: 'SomePlace2',
lBusinessID: 39780,
SystemID: 5000156
},
{
sName: 'SomePlace3',
lBusinessID: 50772,
SystemID: 5000519
},
{
sName: 'SomePlace4',
lBusinessID: 31079,
SystemID: 5000384
},
];
const qrySearchLocID = [{
LocalLabID: '123f',
_ID: 'SomePlace1',
AppLabID: 3,
count: 15
},
{
LocalLabID: '12BC',
_ID: 'SomePlace2',
AppLabID: 3,
count: 40
},
];
var res = qrySearch.map((e, _) =>
(_ = qrySearchLocID.find((q) => q._ID=== e.sName)) ?
{ ...e, ..._ }: e);
console.log(res);
答案 1 :(得分:0)
创建一个包含两个数组的流,按sName
或_ID
对其进行分组,然后将所有组映射到新的合并对象:
const { flow, concat, groupBy, map, merge, partialRight: pr } = _;
const combine = flow(
concat,
pr(groupBy, o => o.sName || o._ID),
pr(map, g => merge({}, ...g))
);
const qrySearch = [{"sName":"SomePlace1","lBusinessID":37343,"SystemID":5000152},{"sName":"SomePlace2","lBusinessID":39780,"SystemID":5000156},{"sName":"SomePlace3","lBusinessID":50772,"SystemID":5000519},{"sName":"SomePlace4","lBusinessID":31079,"SystemID":5000384}];
const qrySearchLocID = [{"LocalLabID":"123f","_ID":"SomePlace1","AppLabID":3,"count":15},{"LocalLabID":"12BC","_ID":"SomePlace2","AppLabID":3,"count":40}];
const result = combine(qrySearch, qrySearchLocID);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
和使用lodash/fp的想法相同:
const { flow, concat, groupBy, map, mergeAll, propOr } = _;
const combine = flow(
concat,
groupBy(o => o.sName || o._ID),
map(mergeAll)
);
const qrySearch = [{"sName":"SomePlace1","lBusinessID":37343,"SystemID":5000152},{"sName":"SomePlace2","lBusinessID":39780,"SystemID":5000156},{"sName":"SomePlace3","lBusinessID":50772,"SystemID":5000519},{"sName":"SomePlace4","lBusinessID":31079,"SystemID":5000384}];
const qrySearchLocID = [{"LocalLabID":"123f","_ID":"SomePlace1","AppLabID":3,"count":15},{"LocalLabID":"12BC","_ID":"SomePlace2","AppLabID":3,"count":40}];
const result = combine(qrySearch, qrySearchLocID);
console.log(result);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>