Pushing information from an embedded array using Typescript

I am encountering an issue when trying to extract data from a nested array. The JSON object I have looks like this:

{
  component: Assembly,
  title: "Assembly",
  path: "/assembly",
  sections: {
      title: "Frame Assembly",
      steps: {
      ["Step 1"]: {
         content:"step 1 content"
      },
      ["Step 2"]: {
        content: "step 2 content"
      }
    },
  },
}

My aim is to use this data to create a navigation system. Below is the function I'm using for this purpose.

private BuildNavigation = (navItem: any) => {

const subSections = [];
const sections = navItem.sections;
  for (const key in sections) {
    if (sections.hasOwnProperty(key)) {
      subSections.push(
        <>
          <ul>
            <li key={key}><ScrollLink to={`${currentPath}#${key}`.toLowerCase()}>{sections[key].title}</ScrollLink></li>
            {Object.getOwnPropertyNames(sections[key].steps).forEach((step: string) => {
                // How do I return step
                console.log(step);
              })}
          </ul>
        </>
      )
    }
  }
return subSections;
}

While I can successfully log the step information, I'm facing difficulty in pushing it into subSections.

I'm hoping for an output similar to the following:

<ul>
   <li>
      <a href="/assembly">Assembly</a>
      <ul style="list-style-type: none; padding: 0px 0px 0px 10px;">
         <li><a href="/assembly#frameassembly">Frame Assembly</a></li>
         <ul>
          <li><a href="/assembly#frameassembly-step-1">Step 1</a></li>
          <li><a href="/assembly#frameassembly-step-2">Step 2</a></li>
         </ul>
      </ul>
   </li>
</ul>

Your assistance will be greatly appreciated.

Answer №1

After adding the line of code:

        <li key={key}><ScrollLink to={`${currentPath}#${key}`.toLowerCase()}>{sections[key].title}</ScrollLink></li>

You can then proceed with the following snippet:

        <ul>
           {
              Object.getOwnPropertyNames(sections[key].steps).map((step: string) => {
                 return (<li>{step}</li>);
              })
           }
        </ul>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combine stream items and calculate the total of a specific field with JQ

I need help organizing multiple items by their names using JSON data. I also want to calculate the total size of each group. To do this, I am utilizing jq for data transformation. Below is the initial raw data: { "fields": { "issuetype ...

What steps can I take to ensure that the airplane is able to cast shadows?

I'm trying to make the plane receive a shadow from the sphere, but I seem to be making a mistake somewhere along the way. Does anyone have any ideas on how to fix it? //.................SETUP................// import { WebGLRenderer, Perspecti ...

What is the best way to toggle between two forms with JavaScript?

I have designed a login screen with two forms inside one div. The first form contains the login box, while the second form is for registration and is hidden using CSS with display:none;. Below the login button, there is a paragraph with an anchor tag to cl ...

The function 'compilation.emitAsset' is not recognized by the sitemap-webpack-plugin

I'm currently working on setting up a sitemap for my live environment and I've encountered an issue while trying to utilize the sitemap-webpack-plugin. The error message I received is as follows: ERROR in TypeError: compilation.emitAsset is not a ...

Unable to fetch information from the controllerAPI function within the tsx file due to a Module Parse error

I am currently working on fetching records from a database using ControllerApi and displaying them through React. The code snippet below is from a file with a *.tsx extension: import React, { useState } from 'react'; import ReactDOM from 'r ...

Fetching an image from Firebase Storage for integration into a Vue application

I am encountering an issue while trying to fetch an image from my firebase storage to display it in my Vue application. The upload process from the app to firebase storage runs smoothly, but there is an error during retrieval. My setup involves using the F ...

The CheckboxTable component in material UI fails to update when there is a change in props

As I develop an admin system, one of the key features I want to implement is the ability to display a list of users in a table format. Additionally, I want to enable bulk actions such as delete and update flags, along with pagination. <CheckboxTable ...

Issues with Google maps are causing multiple maps to malfunction

After incorporating some jquery code to create multiple maps upon window load, I noticed a peculiar issue with the maps - they all display the same location despite having different latitudes and longitudes set. Upon inspecting the code responsible for cr ...

Add up all the numbers within an array by continuously adding them together

I am currently working on a program for my class that is designed to calculate the sum of all integers in an array using recursion. Below is the code snippet I have developed so far: public class SumOfArray { private int[] a; private int n; private int r ...

Undefined ExpressJS variable error

I am currently working on an ExpressJS application where a user can make a POST request to a specific route. This route is designed to look up the ID in MongoDB using req.params.formId. To aid in debugging and understanding the data being returned, I have ...

Incomplete data was retrieved from the localStorage

I am currently in the process of developing a mobile application using Phonegap version 1.4.1. I have encountered an issue on iOS (running on version 5.1) where the app fails to load all data from localStorage. Upon first use of the app, I set a flag in l ...

Printing content using JavaScript on a point of sale (POS) printer with

I need to print a specific div on my POS printer named Optimuz. I have attempted using the following code: <div id="printAreaInvoice" style="visibility: hidden;font-size:8px;width:200px;"></div> < ...

Facing an issue where the data returned in React is showing up as undefined

I am currently facing an issue with passing data down to a component using react router. After confirming that the endpoint is functioning correctly, I have ruled out any server-related problems. Here is the function responsible for fetching page data bas ...

Determine the selected radio button

----EDIT---- I am developing a jQuery mobile application and I need to determine which radio button is selected. This is the JavaScript code I'm using: function filter(){ if(document.getElementById('segment1').checked) { aler ...

How come my array is consistently displaying the identical number for every variable?

I am facing an issue with my for loop that iterates through an array of rectangles. Each time a new rectangle is added, a sprite should be drawn on top of it. The goal is to have each sprite with its own variable that influences how it is drawn. Ideally, ...

Tips on getting Selenium to disregard, skip, or override window.close()

For my Selenium testing in Java, I have encountered a challenge with a webpage script that calls window.close() immediately after the page loads. window.opener='whatever'; window.open('','_parent',&apo ...

What is the best way to establish a limit on the number of characters that can be entered into an input field in a React form?

Currently, I am creating a tool to check the strength of passwords based on their length. However, I am unsure of how to precisely determine if a password falls within specific length ranges such as "between 5 and 10 characters" or "between 20 and 30 cha ...

Angular 4's unique feature is the ability to incorporate multiple date pickers without the

Is there a way to implement a multiple date picker using Angular 4 and TypeScript only? I came across one solution but it seems to only support Angular 1. Thank you in advance! ...

Extract URL fragment using JavaScript

I am looking to convert the hash fragment into an associative array using JavaScript, similar to the $_GET superglobal in PHP. The URL in question is: www.mysite.com/randompage#name=donald&lastname=mclotsoquestions&age=25 Currently, I have the fo ...

Identifying the longest alphabetic word in an array of strings in Java - finding substrings with no symbols or numbers

I am a beginner in learning Java and I have come across an interesting exercise that I need to solve. The task is to find the largest word in a user-inputted string, but with a twist – we should not consider numbers or symbols as words. For example: If ...