Introduction

In today's session, we’re diving into a practical task that involves working with strings to create and manipulate nested objects. Such tasks are common in many real-world applications, where parsing and updating data structures dynamically is crucial. We'll learn how to transform a complex string into a nested object and then update specific values within it. By the end of this lesson, you'll have a solid understanding of string parsing and nested object manipulation.

Task Statement

We need to transform a given complex string into a nested object and update a specific key-value pair within that object. The input string will be in the format "Key1=Value1,Key2=Value2,...". If the value part of a key-value pair contains another key-value string, it should be represented as a nested object.

For example, the input string "A1=B1,C1={D1=E1,F1=G1},I1=J1" should be converted into the following nested object:

let dictionary: Record<string, any> = {
    "A1": "B1",
    "C1": {
        "D1": "E1",
        "F1": "G1"
    },
    "I1": "J1"
};

After parsing this string into the nested object, we'll update the value of the nested key F1 from G1 to a new value, such as NewValue. The function should ultimately return the updated object.

Step Overview

To tackle this problem, we will take the following steps:

  • Initialize Variables and Data Structures: Set up variables and data structures necessary for parsing the string and handling nested objects with TypeScript-specific type annotations.
  • Traverse Input String: Iterate through the input string character by character to identify and handle key-value pairs.
  • Handle Nested Objects: Use a stack to manage and create nested objects dynamically while traversing through the string.
  • Add Key-Value Pairs: Extract keys and values and add them to the appropriate object, whether outer or nested.
  • Update Specific Key-Value: Recursively search through the nested object to locate and update the specified key-value pair.
Setting Up the Function and Variables

Let's start by setting up the function and the necessary variables with type annotations:

function parseString(inputString: string): Record<string, any> {
    const result: Record<string, any> = {};

    let key: string = ""; // to store the current key
    let activeMap: Record<string, any> = result; // to dynamically switch between result and inner objects
    let stack: Array<Record<string, any>> = []; // stack to keep track of objects when nested
    let i: number = 0; // to iterate through the string

Here, we initialize an empty object result with a type that can hold varying key-value structures. A key variable is used for storing the current key being processed, while activeMap helps us dynamically switch between outer and inner objects. The stack is utilized to keep track of nested objects, and i is an index for iterating through the string.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal