Bennett
Bennett

Reputation: 1107

Merge JS sub-objects into a JS object with joined keys

I'm writing template software for publishing node.js modules to GitHub and NPM. A small snippet of an example template:

{{module.name}}

{{module.description}}

Purpose

{{module.code.purpose}}

I have an object built up with all these properties, ex.

{
  "module" : {
    "name" : "test",
    "description" : "Test module"
...

The problem is that I need to merge this object's sub-objects so the final output looks something like this (for replacing the placeholders in the templates):

{
  "module.name" : "test",
  "module.description" : "Test module"
...

So I created my own function to do that:

/**
    Takes an object like
    {
        "type" : "meal",
        "pizza" : {
            "toppings" : ["pepperoni", "cheese"],
            "radius" : 6,
            "metadata" : {
                "id" : 24553
            }
        }
    }
    and changes it to
    {
        "type" : "meal",
        "pizza.toppings" : ["pepperoni", "cheese"],
        "pizza.radius" : 6,
        "pizza.metadata.id" : 244553
    }
*/
const mergeObjectsToKeys = object => {

    // Loop through the passed in object's properties

    return Object.entries(object).reduce((obj, [key, value]) => {

        // Check if it is a sub-object or not

        if (typeof value === "object") {

            // If it is a sub-object, merge its properties with recursion and then put those keys into the master object

            const subObject = mergeObjectsToKeys(value);

            Object.entries(subObject).forEach(([key2, value2]) => {
                obj[key + "." + key2] = value2;
            });
        } else {

            // Otherwise, just put the key into the object to return

            obj[key] = value;
        }
    }, { });
};

Two questions

Upvotes: 0

Views: 348

Answers (1)

guest271314
guest271314

Reputation: 1

One built-in function to handle the requirement is Object.assign(); you can use spread element, Object.entries(), .map() to set property names of object which is property of object.

To handler objects where value is not nested object

let p = Object.keys(o).pop();
let res = Object.assign({}, ...Object.entries(o.module).map(([key, prop]) =>
            ({[`${p}.${key}`]: prop})));

To handle value which is nested object

let o = {
  "type": "meal",
  "pizza": {
    "toppings": ["pepperoni", "cheese"],
    "radius": 6,
    "metadata": {
      "id": 24553
    }
  }
}

let res = Object.assign({}, ...Object.entries(o).map(([prop, value]) => 
            typeof value === "object" && !Array.isArray(value) 
            ? Object.assign({}, ...Object.entries(value)
              .map(([key,val]) => ({[`${prop}.${key}`]: key})))
            : {[prop]: value})
          );

console.log(res);

Upvotes: 2

Related Questions