Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript array of objects - remove duplicate objects from an array based on nested object

I have an array of objects with nested category objects (see below). I would like to remove the duplicate objects (based on "objectID") and create a new array with only the objects that have a higher amount of nested "hierarchicalCategories" key value pairs

const objectsArray = [

{
  "objectID": 1234,
  "hierarchicalCategories": {
    "lvl0": "Women's",
    "lvl1": "Women's > Shirts",
    "lvl2": "Women's > Shirts > Tees"
  }
},
{
  "objectID": 5678,
  "hierarchicalCategories": {
    "lvl0": "Men's"
  }
},
{
  "objectID": 1234,
  "hierarchicalCategories": {
    "lvl0": "Women's"
  }
},
{
  "objectID": 5678,
  "hierarchicalCategories": {
    "lvl0": "Men's",
    "lvl1": "Men's > Shoes"
  }
}

]

So the expected result would look like this: The final array would filter duplicates and keep one instance of each object...the "objectID" : 1234 instance which had "hierarchicalCategories" up to "lvl2" and the "objectID" : 5678 instance which had "hierarchicalCategories" up to "lvl1"

const newArray = [

{
  "objectID": 1234,
  "hierarchicalCategories": {
    "lvl0": "Women's",
    "lvl1": "Women's > Shirts",
    "lvl2": "Women's > Shirts > Tees"
  }
},
{
  "objectID": 5678,
  "hierarchicalCategories": {
    "lvl0": "Men's",
    "lvl1": "Men's > Shoes"
  }
}

]

I have this function which works for a new array based on filtering duplicate objectID's, but I am not sure how to create logic to keep the object with more "hierarchicalCategories" key value pairs.

     const newArray = Array.from(new Set(objectsArray.map(a => a.objectID)))
                .map(objectID => {
                    return objectsArray.find(a => a.objectID === objectID)
         })
like image 840
mattylight10 Avatar asked Jun 18 '26 02:06

mattylight10


1 Answers

You could take a Map and assign the categories to the same group.

const
    objectsArray = [{ objectID: 1234, hierarchicalCategories: { lvl0: "Women's", lvl1: "Women's > Shirts", lvl2: "Women's > Shirts > Tees" } }, { objectID: 5678, hierarchicalCategories: { lvl0: "Men's" } }, { objectID: 1234, hierarchicalCategories: { lvl0: "Women's" } }, { objectID: 5678, hierarchicalCategories: { lvl0: "Men's", lvl1: "Men's > Shoes" } }],
    result = Array.from(objectsArray
        .reduce((m, o) => {
            if (m.has(o.objectID)) {
                Object.assign(m.get(o.objectID).hierarchicalCategories, o.hierarchicalCategories);
                return m;
            }
            return m.set(o.objectID, o);            
        }, new Map)
       .values()
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

A shorter approach by collecting the categroies directly and building new object from the collected parts.

const
    objectsArray = [{ objectID: 1234, hierarchicalCategories: { lvl0: "Women's", lvl1: "Women's > Shirts", lvl2: "Women's > Shirts > Tees" } }, { objectID: 5678, hierarchicalCategories: { lvl0: "Men's" } }, { objectID: 1234, hierarchicalCategories: { lvl0: "Women's" } }, { objectID: 5678, hierarchicalCategories: { lvl0: "Men's", lvl1: "Men's > Shoes" } }],
    result = Array.from(
        objectsArray.reduce((m, { objectID: id, hierarchicalCategories: o }) => 
            m.set(id, Object.assign((m.get(id) || {}), o)), new Map),
       ([objectID, hierarchicalCategories]) => ({ objectID, hierarchicalCategories })
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 62
Nina Scholz Avatar answered Jun 20 '26 16:06

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!