简体   繁体   中英

How do group elements in an array of objects of different sizes by comparing two different unique properties? in javascript

I have two different array of objects that i get from an aggregate function because I am using two different collections.

I have tried using the map function like discribed here, but it does not solve my problem what else can i do to get the desired results?

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

and

qrySearchLocID = [{
    LocalLabID: '123f',
    _ID: 'SomePlace1',
    AppLabID: 3,
    count: 15   
  },
  {
    LocalLabID: '12BC',
    _ID: 'SomePlace2',
    AppLabID: 3,
    count: 40
  }
];

after trying the result is only this array :

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
  },
]

for simplicity ive kept the array short. I need to compare the _ID if it matches sName for desired output:

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

I've tried using this example:

var result = qrySearch.map((e, _) => 
          (_ = qrySearchLocID.find((q) => q._ID=== e.sName)) ? 
          { ...e, ...{ _ID: _._ID} } : e);

but the count is missing from results.

You can just spread your placeholder _ too to merge the values

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

Create a flow that concats the two arrays, groups them by sName or _ID , and maps all groups to a new merged object:

 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>

And the same idea using 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>

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