How to use Recursion In Typescript

Recursion is a programming technique that is used to solve different problems. In this technique, a function calls itself again and again until the terminating condition met. We can use this to flatten a nested object in TypeScript.

Approach:

  • Create a function as flattenObject that takes an object as a parameter and returns it as a flattened object. Inside the function, we loop through each property of the input object to check its type.
  • If it is an object, we recursively call the flattenObject function on that object. Otherwise, we simply store the value in the resultant object.
  • After the given loop ends, we return the object as a result.
  • Finally, we call the created function by passing the given response object inside.

Example: The below example illustrates the above approach to flatten the nested objects in TypeScript.

Javascript
let obj = {
    company: "w3wiki",
    members: {
        John: "USA",
    },
    technology: {
        language: "HTML, CSS",
        library: {
            name: "Node JS",
        },
    },
};

const flattenObject = (obj: any): any => {
    let resultObj: any = {};

    for (const i in obj) {
        if (typeof obj[i] === 'object' &&
            !Array.isArray(obj[i])) {
            // Recursively invoking the funtion 
            // until the object gets flatten
            const tempObj = flattenObject(obj[i]);
            for (const j in tempObj) {
                resultObj[i + '.' + j] = tempObj[j];
            }
        } else {
            resultObj[i] = obj[i];
        }
    }
    return resultObj;
};

const flattenedObject = flattenObject(obj);
console.log(flattenedObject);
console.log("Accessing flattened properties: ")
console.log(flattenedObject['technology.language']);
console.log(flattenedObject['technology.library.name']);

Output:

company: "w3wiki"
members.John: "USA"
technology.language: "HTML, CSS"
technology.library.name: "Node JS"
Accessing flattened properties:
HTML, CSS
Node JS

How to Flatten Dynamically Nested Objects in Order in TypeScript ?

We are required to flatten a Typescript nested object that contains arrays and objects as children such that there should be no nested children left and everything should be at the same height.

Example:

Input: obj = {
    subject: "Computer Networks",
    students: {
        Jake: "USA"
    }
};
Output: obj = {
    "subject": "Computer Networks",
    "students.Jake": "USA"
}

Table of Content

  • Using Recursion
  • Using the Lodash library

Similar Reads

Using Recursion

Recursion is a programming technique that is used to solve different problems. In this technique, a function calls itself again and again until the terminating condition met. We can use this to flatten a nested object in TypeScript....

Using the Lodash library

In this approach, we will use lodash library provided by NPM to flatten the given object. It provides several functions such as flatten, flattenDeep, and flatMap which allow us to recursively flatten nested objects and arrays....

Custom function without external libraries

In this approach, we’ll create a custom function to recursively flatten the nested object. This approach allows us to understand the logic behind flattening nested objects without relying on external libraries....

Contact Us