Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dcranmer's avatar

Merge objects based on unique matching property

I have two objects: majors (Object1) and majorsWithFilters (Object2); each contains a list of objects.

In order to perform filtering operations,, I need to merge the sub-objects within each in order to bring in the "filters" property (Object2) into an Object1 object whose "guideLink" property matches the key of the Object2 object. (I wish these objects had ids I could match; but right now, the only matching property is the url in the guideLink property (Object1) and the key (Object2). Having the filters set as text strings is also not optimal.

I know , that's a mouthful.

Object1 contains a series objects structured like this:

African Cultural Studies:Object
degreeTypes:Array[2]
0:Object
type:"BA"
url:"https://aLongURL"
1:Object
type:"BS"
url:"aLongURL"
guideLink:"URL-matches-key-of-Object2-sub-object"
title:"African Cultural Studies"

Object2 contains objects structured like this:

https://URL-matchesguideLink-of-Object1-sub-object:"filter_25 filter_30 filter_31 filter_35"

The key in each Object2 object matches the "guideLink" property in one Object1 sub-object. The goal is to merge these so that the Object1 subjects look like this (with a filters property containing an array of filter numbers):

African Cultural Studies:Object
degreeTypes:Array[2]
0:Object
type:"BA"
url:"https://aLongURL"
1:Object
type:"BS"
url:"aLongURL"
guideLink:"aLongURL"
title:"African Cultural Studies"
filters: Array[2]
0: 25,
1: 30
0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

One possible solution is to loop through each object in Object1 and check if its "guideLink" property matches a key in Object2. If there is a match, then extract the "filters" property from the corresponding Object2 object and add it to the Object1 object as a new "filters" property. Here's an example implementation in JavaScript:

for (let obj1 of Object.values(majors)) {
  const guideLink = obj1.guideLink;
  if (majorsWithFilters.hasOwnProperty(guideLink)) {
    const filtersStr = majorsWithFilters[guideLink];
    const filtersArr = filtersStr.split(' ').map(str => parseInt(str.replace('filter_', '')));
    obj1.filters = filtersArr;
  }
}

Explanation:

  • We use a for...of loop to iterate through each object in Object1 (majors).
  • For each object, we extract its "guideLink" property and check if it exists as a key in Object2 (majorsWithFilters) using the hasOwnProperty method.
  • If there is a match, we extract the "filters" property from the corresponding Object2 object (which is a string of space-separated filter numbers), split it into an array of integers using split and map, and assign it to a new "filters" property in the Object1 object.
  • Note that we use parseInt and replace to convert the filter strings from "filter_XX" format to integers.

This should result in the desired merged objects with "filters" properties.

Please or to participate in this conversation.