How to combine two array of objects of different sizes, based on a property in Javascript?

CRod

I have two arrays of objects that are different in length but share similar information.

qrySearchLocID = [{
    LocalLabID: '123f',
    SystemID: 5000152,
    AppLabID: 3
  },
  {
    LocalLabID: '12BC',
    SystemID: 5000384,
    AppLabID: 3
  },
];

and

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

I want to combine these two arrays based on the SystemID, copy all the information from qrySearch and add the LocalLabID from qrySearchLocID and nothing else. For example I want the result array to be

[{
    sName: 'SomePlace1',
    lBusinessID: 37343,
    SystemID: 5000152,
    LocalLabID: '123f'
  },
  {
    sName: 'SomePlace2',
    lBusinessID: 39780,
    SystemID: 5000156
  },
  {
    sName: 'SomePlace3',
    lBusinessID: 50772,
    SystemID: 5000519
  },
  {
    sName: 'SomePlace4',
    lBusinessID: 31079,
    SystemID: 5000384,
    LocalLabID: '12BC'
  },
]

Thanks in advance.

Ele

You can use map and find functions.

var qrySearchLocID = [{
    LocalLabID: '123f',
    SystemID: 5000152,
    AppLabID: 3
  },
  {
    LocalLabID: '12BC',
    SystemID: 5000384,
    AppLabID: 3
  },
];

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

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

console.log(result);

Resources

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Two different array in JavaScript and combine Single array

How to combine two array of objects where key name is different but the corresponding value is same?

How to combine two javascript FormData objects

JavaScript filter array of objects based on property values

Javascript: two different objects sharing an array property?

Matching a set of objects within an array based on a different array of search property values in Javascript

how to combine two different array of objects into one new array of object in javascript?

How to combine two arrays into an array of objects in javascript?

How to combine two dataframe with different sizes of row?

How to combine two LSTM layers with different input sizes in Keras?

Combine array of objects inner array based on property

Get list of duplicate objects in an array based on two property in javascript

Swift - How to combine two unordered arrays of structs into an array of new objects based on matching values?

How to find overlap of two array based of an objects property in ruby

How to combine two lists of different sizes

how to combine two array of Objects in javascript without using Underscore or Lodash?

How to combine two array of objects into one array based on key match values

how to combine two different arrays to associative array?

How to match values from different array of objects based on a property?

How to combine array elements of two array in JavaScript?

Remove objects from array based on their property with javascript

check if two objects in array are adjacent based on property

Combine different Objects in Array

Javascript array of objects merge based on the object property

How to filter array of objects by different properties based on array that declare what property to check?

Two objects of different sizes and $null

How to combine two arrays into an array of objects?

How to filter a different item value of two array of objects in JavaScript?

How to combine two arrays into one array of objects?