如何通过比较两个不同的唯一属性对不同大小的对象数组中的元素进行分组?在JavaScript中

时间:2019-02-15 04:50:08

标签: javascript mongodb typescript lodash

我有两个不同的对象数组,它们是从聚合函数中获取的,因为我使用的是两个不同的集合。

我已尝试使用此处介绍的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);

,但结果缺少计数。

2 个答案:

答案 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>