Navigating within a mapping array

I am attempting to create a nested map within a looped map. However, as this is my first time using JavaScript, I am finding it a bit challenging to use the correct tags. My goal is to create a table where the first row displays the category and the second row displays the items associated with each category. I have already created the thead section, but I am encountering an issue with the array.map function. Here is what I have tried so far:

     {posts.map((category)=>  (
      <tr>
                 
    <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
       
      </td>
    
    <td   className={
          "px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left " +
          (color === "light"
            ? "bg-red-50 text-red-500 border-red-100"
            : "bg-lightBlue-800 text-lightBlue-300 border-lightBlue-700")
        }>
       {category.name}
      </td> 
      </tr> 
                   {
                   posts.filter((post)=>post.type_id===category.id).map((post) => (                   
                   <tr key={ post.id }>
                 
                     <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                     {post.id} 
                     </td>
                   
                     <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                     {post.mapel}
                     </td>
                    
                     <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                     {post.class1}
                     
             </td>
                     <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                     {post.class2}
                     
                     </td>
                     <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                     {post.class3}
                    
                    </td>
                    <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                    {post.class4}
                    
                    </td>
                    <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                    {post.class5}
                    
                    </td>
                    <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                    {post.class6}
                    
                    </td> <td>
               <button onClick={() => deletePost(post.id)} variant="danger" size="sm">DELETE</button>
  
               </td>
                    </tr> ))}
                 
                   )
                   
              )}

I am encountering an error in the second array.map function, and I'm not sure where I've gone wrong. Perhaps someone can assist me. Thank you.

A friend suggested that I add HTML tags, but this creates a new table instead of continuing from the initial thead section I created...

My desired outcome is to create a table like the following:

No | Name | Class 1 | Class 2 |

Category 1

1 | ItemA| 5 | 2 | (associated with category ID number 1)

2 | ItemB| 5 | 2 |

Category 2 

3 | ItemC| 5 | 2 | (associated with category ID number 2)

Answer №1

and transform it to look like this.

{ posts.map((category) => {
              return(<>
                  <tr>
                      <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left"></td>
                      <td className={
                      "px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left " +
                      (color === "light" ? "bg-red-50 text-red-500 border-red-100" : "bg-lightBlue-800 text-lightBlue-300 border-lightBlue-700")}>
                      {category.name}
                      </td> 
                  </tr></>)
              }).posts.filter((post) => post.tipe_aka === category.id).map((post) => {
                  return(<>             
                 <tr key={ post.id }>
               
                      <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                      {post.id} 
                      </td>
                  
                      <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                      {post.mapel}
                      </td>
                      
                      <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                      {post.class1}
                      </td>

                      <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                      {post.class2}
                      </td>

                      <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                      {post.class3}
                      </td>

                      <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                      {post.class4}
                      </td>

                      <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left">
                      {post.class5}
                      </td>

                      <td className="px-6 align-middle border border-solid py-3 text-xs uppercase border-l-0 border-r-0 whitespace-nowrap font-semibold text-left"> 
                      {post.class6}
                      </td> 
                      
                      <td>
                          <button onClick={() => deletePost(post.id)} variant="danger" size="sm">DELETE</button>
                      </td>
           
                  </tr></>
                  )}) 
      }

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

How can I configure Helmet JS CSP to permit external CSS and JavaScript scripts? I'm also wondering about the whereabouts of the inline

I'm fairly new to the world of JavaScript and ExpressJS. My goal is to create a web application with a working login page. I'm currently in the process of setting up the helmet JS middleware to allow for external CSS and scripts. However, despite ...

Preserving classes in JQuery after making AJAX requests

Before we proceed, it's important to note that I am unable to modify any of the existing calls, code, or functions. This means I must come up with a workaround solution. So, here's the situation: I have a form containing various fields and a dro ...

Using reduce in JavaScript to form a fresh object

I've been struggling with creating an object using reduce from a nested array. The task is to generate a new object with keys named _uid and values matching the initialValue from the objects that contain both properties. I have written a function that ...

Generate an interactive table: one segment featuring a title or tag - its corresponding rows and columns that change dynamically, followed by another similar segment

I'm attempting to generate a dynamic table with the following structure upon clicking a button (I can't include an image, so here is a sample code to illustrate what I'm aiming for, but I need everything to be dynamic in the actual scenario) ...

What is the most effective way to stop zooming when focusing on form fields on iOS or similar devices when using font sizes of 16px or lower?

It appears that many proposed solutions involve changing the text to 16px or using JavaScript to determine if the phone is an iPhone. However, altering the style may not be the most practical solution and checking for specific devices like iPhones could ...

Node.js npm-migration enables the creation of multiple tables in a single migration process

I am new to npm-migration for nodejs and I am exploring ways to streamline the process of creating multiple tables using a single migration, rather than having separate migrations for each table creation. I have experimented with the following code: "up": ...

What are the steps for implementing this javascript code in an HTML document?

Recently, I've been seeking advice on how to address the issue of wanting a button click to automatically select the search box on my website. Suggestions have pointed towards using Javascript for this functionality, with the following code recommend ...

I am puzzled as to why the number displayed by my array_sum function is incorrect

I have encountered a puzzling issue with an array manipulation task: $sustainCapital_arr = Array ( [0] => 2,759 [1] => 3,269 [2] => 3,481 [3] => 3,573 [4] => 3,997 [5] => 4,421 [6] => 10,999 ) When trying to calculate the sum of the v ...

What is the best way to transition from the splash screen to the onboarding screens?

Welcome to SplashScreen.js I am here to help you display a splash screen that will disappear after a set timeout, and then smoothly navigate you to the Onboarding Screen. import React from 'react'; import { View} from 'react-native'; ...

Unexpected token error occurs when making cross-domain AJAX requests to the server and receiving a JSON object

I have set up an express server to handle get requests to specific url endpoints. When responding to these requests, I am sending back data in JSON format to enable making Ajax calls and retrieving data from the page. To allow for cross-domain requests, I ...

discord.js: Imported array not displaying expected values

I've been facing an issue with accessing elements from an imported array. Even though the array is successfully imported, attempting to access its elements using [0] results in undefined. Here's how I exported the array in standList.js: exports. ...

What is the best way to send a file and retrieve a value on Internet Explorer versions 8 and 9?

Greetings everyone, I am encountering a technical issue that has consumed a significant amount of my time. I am hopeful that you may be able to assist me with resolving it. In my table, I have a list of files along with corresponding document types and de ...

Having trouble with the Ajax load function not functioning in your HTML code?

The issue has been resolved. Thank you to everyone who helped! Initially, I was attempting to run a file offline instead of on a web server (XAMPP server). Once I uploaded the file to the web server, it started working properly. I had been trying to load ...

What is the process for converting data extracted from an array into numerical values in Python?

I have a function that reads data from a txt file and returns three different arrays. def extract_data(filename): infile= open('approx_derivative_sine.txt', 'r') dx = [] abserror = [] n = [] for line in infile: ...

Tips for showcasing a table generated from various input types on a separate page after pressing the submit button

After creating two JavaScript functions, I am eager to utilize both of them upon pressing the submit button on my form. The first function is already integrated into the submit button and activates a paragraph display upon submission. Now, I intend to sh ...

An effective method for retrieving textarea data in Node.js

I am facing an issue where I cannot successfully send data from a <textarea> to Node.js. It seems that the data I'm trying to send is not being received by Node.js. To retrieve data in Node.js: continueBtn.addEventListener("click", ...

Is it possible to selectively export certain interfaces within a .d.ts file?

// configuration.d.ts export interface Configuration { MENU_STRUCTURE: Node[]; } interface Node { name: string; } Looking at the snippet above, I am aiming to only export Configuration. However, I noticed that I can also import Node from an ext ...

Organizing lists with HTML unordered lists

Is it possible to sort list items by numbers within a strong tag using JavaScript code? The current code successfully sorts the numbers, but removes them from the div tag. (The JavaScript code used below is for sorting by Name and works properly when &apos ...

Using Vue to turn a string into a mathematical operation

I'm in the process of creating a basic calculator using Vue, but I'm stuck on how to convert a string into a mathematical operation. I've already written the code for building the strings. <template> <div> <input type=&q ...

What is the process behind managing today's Google image of the day?

After coming across the javascript picture control on the Google search page, I became interested in replicating it myself. The feature zooms in on a picture when hovering over it, but I couldn't locate the HTML code they were using for it. Is there ...