Recursive Nesting of Linked JavaScript Objects

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore a JavaScript function called nest that recursively nests objects linked to one another in a flat array. The function uses recursion, Array.prototype.filter(), and Array.prototype.map() to filter and nest the items based on their id and parent_id properties. By the end of this lab, you will have a better understanding of how to use recursion to create nested objects in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/DOMManipulationGroup(["`DOM Manipulation`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/destr_assign("`Destructuring Assignment`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") javascript/DOMManipulationGroup -.-> javascript/dom_traverse("`DOM Traversal`") subgraph Lab Skills javascript/variables -.-> lab-28507{{"`Recursive Nesting of Linked JavaScript Objects`"}} javascript/data_types -.-> lab-28507{{"`Recursive Nesting of Linked JavaScript Objects`"}} javascript/arith_ops -.-> lab-28507{{"`Recursive Nesting of Linked JavaScript Objects`"}} javascript/comp_ops -.-> lab-28507{{"`Recursive Nesting of Linked JavaScript Objects`"}} javascript/array_methods -.-> lab-28507{{"`Recursive Nesting of Linked JavaScript Objects`"}} javascript/higher_funcs -.-> lab-28507{{"`Recursive Nesting of Linked JavaScript Objects`"}} javascript/destr_assign -.-> lab-28507{{"`Recursive Nesting of Linked JavaScript Objects`"}} javascript/spread_rest -.-> lab-28507{{"`Recursive Nesting of Linked JavaScript Objects`"}} javascript/dom_traverse -.-> lab-28507{{"`Recursive Nesting of Linked JavaScript Objects`"}} end

How to Nest Objects Using Recursion in JavaScript

To nest objects in a flat array recursively, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use recursion to nest objects that are linked to one another.
  3. Use Array.prototype.filter() to filter the items where the id matches the link.
  4. Use Array.prototype.map() to map each item to a new object that has a children property which recursively nests the items based on which ones are children of the current item.
  5. Omit the second argument, id, to default to null which indicates the object is not linked to another one (i.e. it is a top level object).
  6. Omit the third argument, link, to use 'parent_id' as the default property which links the object to another one by its id.

Here's the code:

const nest = (items, id = null, link = "parent_id") =>
  items
    .filter((item) => item[link] === id)
    .map((item) => ({ ...item, children: nest(items, item.id, link) }));

To use the nest() function, create an array of objects that have an id property and a parent_id property that links them to another object. Then, call the nest() function and pass the array as an argument. The function will return a new array of objects that are nested based on their parent_id property.

For example:

const comments = [
  { id: 1, parent_id: null },
  { id: 2, parent_id: 1 },
  { id: 3, parent_id: 1 },
  { id: 4, parent_id: 2 },
  { id: 5, parent_id: 4 }
];
const nestedComments = nest(comments);
// [{ id: 1, parent_id: null, children: [...] }]

Summary

Congratulations! You have completed the Nest Objects lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like