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

Csaba

I have an array of nested objects, and I would like to simplify it a bit and convert it to an object with key, value pairs, so the result would be like this:

Simple Object:

{Groceries: {
    'Organic eggs': null,
    Fruits: {
        Apple: null,
        Berries: {
            Blueberry: null,
            Raspberry: null
        }
    }
}}

Array of nested objects:

const tree = [{
    "id": 1,
    "name": "Groceries",
    "parentId": null,
    "children": [{
            "id": 3,
            "name": "Organic eggs",
            "parentId": 1,
            "children": []
        }, {
            "id": 5,
            "name": "Fruits",
            "parentId": 1,
            "children": [{
                    "id": 6,
                    "name": "Apple",
                    "parentId": 5,
                    "children": []
                },{
                    "id": 7,
                    "name": "Berries",
                    "parentId": 5,
                    "children": [{
                            "id": 8,
                            "name": "Blueberry",
                            "parentId": 7,
                            "children": []
                        },{
                            "id": 9,
                            "name": "Raspberry",
                            "parentId": 7,
                            "children": []
                        }]
                }]
        }]
}]
Nick Parsons

Using recursion, you can pass your array tree and loop through it. If you encounter an object in your tree array which has children, you can pass this child array back to your function to build a new simplified object from this sub-array. If the length of the object's children property is 0, then you can set the simplified object's name property to be null.

See example below:

const tree = [{ "id": 1, "name": "Groceries", "parentId": null, "children": [{ "id": 3, "name": "Organic eggs", "parentId": 1, "children": [] }, { "id": 5, "name": "Fruits", "parentId": 1, "children": [{ "id": 6, "name": "Apple", "parentId": 5, "children": [] }, { "id": 7, "name": "Berries", "parentId": 5, "children": [{ "id": 8, "name": "Blueberry", "parentId": 7, "children": [] }, { "id": 9, "name": "Raspberry", "parentId": 7, "children": [] }] }] }] }];

const simplifyObj = arr => {
  const obj = {};
  for(const {name, children} of arr)
    obj[name] = children.length ? simplifyObj(children) : null;
  return obj;
}

const simple = simplifyObj(tree);
console.log(simple);
.as-console-wrapper { max-height: 100% !important;} /* ignore */

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert array of "key-value" pairs into object using javascript

How to convert an Object {} to an Array [] of key-value pairs in JavaScript

How to convert an Object {} to an Array [] of key-value pairs in JavaScript

Convert JavaScript array of 2 element arrays into object key value pairs

How to convert an array of objects to object with key value pairs

How to convert string into object with key value pairs

how to convert array of objects to key value pairs in php

Flatten nested array with key value pairs in Javascript

How do I turn key value pairs in object into array of objects?

Convert array of "key-value" pairs into object using javascript

Convert array of objects to object of key-value pairs

typescript or JavaScript convert nested array of objects to key,value pair

Javascript: How to make array from object with key value pairs as strings?

How do you convert an array of {"name":"myName","value":"myValue"} objects into an object of "myName":"myValue" pairs in JavaScript?

convert an array and an array of arrays into array of objects with key value pairs

Convert array of single-property JavaScript objects to array of key/value pairs

How to convert object containing key-value pairs to an array of objects in JavaScript

How to convert an array into an object in javascript with mapped key-value pairs?

Convert array of objects to comma separated key value pairs javascript

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

Convert nested array of objects to object of key-value pairs using reducer

convert javascript object into array of objects based on key value array

javascript convert object to array of objects with key value labels

How to convert array to an object with key/value pairs Javascript

convert an array to an object of key value pairs

How to convert an array of objects into an array of objects with mapped key-value pairs

JS - How to add key:value pairs from objects nested in arrays to other objects nested in another array

How to parse an objects with array index to object array with key value pairs

Map array of objects to object of key value pairs

TOP Ranking

HotTag

Archive