Convert Array of Objects to Nested Object in Javascript

Julien

I would like to convert an array of objects to a nested object using a recursive function.

The objective is to create a function that works whatever the depth of my inital array. You can see below the initial data with desired result + code snippet with attempt at solving this issue.

Initial array of objects

configurator: [
  {
    key: '-LLnLuLt6cn-vBpMWv-u',
    name: 'CONFIGURATOR_1',
    collections: [
      {
        key: '-LLnMWy69vACjys0QIGH',
        name: 'COLLECTION_1',
        options: [
          {
            key: '-LLnOxg5hsDYR-PcfjBT',
            name: 'OPTION_1',
          },
          {
            key: '-LLnP-O6TyHxIpPk9bCU',
            name: 'OPTION_2',
          },
        ],
      },
      {
        key: '-LLnMYNyJmhSCPB-8lL1',
        name: 'COLLECTION_2',
      },
    ],
  },
  { key: '-LLnLtLs7PjXSAW0PWCQ',
    name: 'CONFIGURATOR_2',
  }]

Desired outcome : Nested objects

configurator: {
  '-LLnLuLt6cn-vBpMWv-u': {
    name: 'CONFIGURATOR_1',
    index: 0,
    collections: {
      '-LLnMWy69vACjys0QIGH': {
        name: 'COLLECTION_1',
        index: 0,
        options: {
          '-LLnOxg5hsDYR-PcfjBT': {
            name: 'OPTION_1',
            index: 0,
          },
          '-LLnP-O6TyHxIpPk9bCU': {
            name: 'OPTION_2',
            index: 1,
          },
        },
      },
      '-LLnMYNyJmhSCPB-8lL1': {
        name: 'COLLECTION_2',
        index: 1,
      },
    },
  },
  '-LLnLtLs7PjXSAW0PWCQ': {
    name: 'CONFIGURATOR_2',
    index: 1,
  },
}

My attempt

Here is a code snippet of what I have tried so far. It only works with the first depth of the array. I believe this is the challenge to solve: how to dynamcilly add/'push' an object to a nested object ?

Hope someone can help. Cheers, Julien.

const data = {
  configurator: [{
      key: '-LLnLuLt6cn-vBpMWv-u',
      name: 'CONFIGURATOR_1',
      collections: [{
          key: '-LLnMWy69vACjys0QIGH',
          name: 'COLLECTION_1',
          options: [{
              key: '-LLnOxg5hsDYR-PcfjBT',
              name: 'OPTION_1',
            },
            {
              key: '-LLnP-O6TyHxIpPk9bCU',
              name: 'OPTION_2',
            },
          ],
        },
        {
          key: '-LLnMYNyJmhSCPB-8lL1',
          name: 'COLLECTION_2',
        },
      ],
    },
    {
      key: '-LLnLtLs7PjXSAW0PWCQ',
      name: 'CONFIGURATOR_2',
    }
  ]
};


const format = (object) => {
  const result = {};
  Object.keys(object).forEach((property) => {
    if (Array.isArray(object[property])) {
      object[property].forEach((test, index) => {
        const {
          key,
          ...content
        } = test;
        result[key] = {
          index,
          ...content
        };
        format(content);
      });
    }
  });
  return result;
};

const formated = format(data);
console.log('@FORMATED__', formated);

Luis Vasquez

Edited

function format(data) {
  var result = {};

  data.forEach(function(config, i) {
    var obj = {
        index: i
    }

    Object.keys(config)
        .forEach(function(key) {
            if (!Array.isArray(config[key])) {
                if (key != "key") obj[key] = config[key];
            } else {
                obj[key] = format(config[key]);
            }
        });

    result[config.key] = obj;
  });

  return result;
}
console.log(format(data));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Create nested object from array of objects in JavaScript

Convert Nested Object into Array of Objects and remove keys

Javascript - Convert Nested Object to Array of items

Unable to convert a nested object into an array of objects and viceversa

How to convert array into nested objects in javascript

How to Convert nested json to array of object in Javascript?

How to convert array of objects to object of array in javascript?

Filtering a nested array with objects by checking another nested array with object - JavaScript

Convert array of objects with nested array of objects into array-like object

JavaScript: Comparing arrays of objects with nested array of object

Javascript convert Object with child-objects into Array

Convert Javascript array of objects into one object

how to convert objects in array which is already in a nested object in javaScript

JavaScript: How to convert nested array of object to key-value pair objects

How to convert an array of nested objects to an object with key, value pairs in JavaScript

How to get array object in nested array of array of objects javascript

Convert an array of nested objects to another nested index object

is there any way to convert nested object to array in javascript

how convert my nested object to array in javascript

Javascript Replacing nested array of objects with updated object

How to convert array of objects into nested object in MongoDB aggregation pipeline

How to convert nested object to array of object in javascript?

How to convert nested array of objects into one array of objects using javaScript?

How to convert an object to array of multiple objects in javascript

Convert array of objects to nested object based on values

Convert array to nested object structure using Javascript

Convert nested Array of Objects to one dimensional Array of Objects Javascript/React

Convert nested objects into an array of objects

Convert object of objects to array of objects in javascript