Convert an array of JSON objects into a grid formatted time table using the

I am using Next.js 10 to create a timetable or schedule similar to the one below:

bus stop time 1 time 2 time 3
{props[0].bus stop} {props[0].times[0]} {props[0].times[1]} {props[0].times[2]} ...
{props[1].bus stop} {props[1].times[0]} {props[1].times[1]} {props[1].times[2]} ...
{props[2].bus stop} {props[2].times[0]} {props[2].times[1]} {props[2].times[2]} ...
... ... ... ... ...

JSON data:

[
   {
      "bus stop":"Bad Königshofen ZOB",
      "times":[
         "05:55",
         "06:35",
         "06:35",
         "NULL",
         ...
      ]
   },
   {
      "bus stop":"Bad Königshofen Schulzentrum",
      "times":[
         "NULL",
         "NULL",
         "12:17",
         "13:10",
         ...
      ]
   },
   {
      "bus stop":"Großeibstadt",
      "times":[
         "06:00",
         "06:40",
         "06:40",
         "NULL",
         ...
      ]
   }
]

A solution for generating "bus stop" rows:

{props.map(prop => <div className={styles.grid_left}>{prop["bus stop"]}</div>)}

I am unsure how to generate the "times" section in the table.

For clarity: "Bus stops" represent the locations where the bus goes, while "times" indicate when the bus arrives at each stop.

If my JSON structure is not ideal, I am open to changing it. However, the "bus stops" and "times" should be capable of any length, whether it's 1 or 2 million entities.

Edit (credited to @yochanan sheinberger):

{props.map(prop => 
  <grid className={styles.grid_container}>
    <div className={styles.grid_left}>{prop["bus stop"]}</div>
    {prop.times.map(time => <div className={styles.grid_right}>{time}</div>)}
  </grid>
)}

My custom CSS solution:

<table className={styles.tg}>
  {props.map(prop =>
    <tr>
      <th className={styles.tg_0pky}>{prop["bus stop"]}</th>
      {prop.times.map(time => time === "NULL" ? <th className={styles.tg_0pky}></th> : <th className={styles.tg_0pky}>{time}</th>)}
    </tr>
  )}
</table>

CSS styling:

.tg {
  border-collapse: collapse;
  border-spacing: 0;
}

.tg td {
  border-color: black;
  border-style: solid;
  ...

This will result in a structured table display as shown above.

Answer №1

Perhaps some adjustments will be necessary, but this serves as a starting point.

{props.map(prop => {
 <div>
  <div className={styles.grid_left}>{prop.bushaltestelle}</div>
  {prop.zeiten.map(zeit => <div>{zeit}</div>)}
 </div>
)}

Alternatively, you can use a table element:

<table>
  {props.map((prop, i) => 
    <>
      {i === 0 && <tr><th></th>{prop.zeiten.map((z, i) => <th>{"zeit" + i}</th>)}</tr>}
      <tr>
        <th >{prop.bushaltestelle}</th>
        {prop.zeiten.map(zeit => <td>{zeit !== "NULL" ? zeit : "-"}</td>)}
      </tr>
    </>
  )}
</table>

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

The iFrame that is generated dynamically becomes null when accessed from a page that has been loaded using JQuery

One issue I am facing is with a dynamically created iframe in regular javascript. It functions perfectly fine when called from a static page using conventional methods. However, when it is being called from a page loaded by jQuery, I encounter an error s ...

Best methods for deleting an element's attribute or style attribute

Imagine this snippet of CSS code: .notif-icon { display: inline-block; } In my HTML, I have the following element which starts off hidden by overriding the display property of the notif-icon class: <span id="error" class="notif-icon& ...

The real-time updates on an Angular 2 website can be seen across multiple devices simultaneously

Just getting started with Angular 2 and ran into an interesting issue. When setting up my website, NPM defaults the server to http://localhost:3000. To test the site on another computer, I tried accessing it using my IP address http://10.x.x.x:3000 and eve ...

I am seeking assistance with my code. It is passing most of the test cases, but is failing only two without any error messages. What could

I recently started teaching myself programming, so please excuse me if my question seems a bit basic. One of the challenges on CodeCamp requires defining a function that takes an array with 2 values as input and returns the Least Common Multiple (LCM) of ...

Are the intervals constantly shifting?

I have encountered an issue in my code where if an element is not currently doing something (specifically, "playing"), then I initiate the playing sequence. However, if it is already playing, I stop the action. While I have successfully implemented the sta ...

Laravel - Error in Symfony\Component\HttpFoundation\JsonResponse Argument 2

How can I retrieve the data in $users as an array and the data in $this->guard()->user() separately without it being inside an array? I attempted the following, but encountered an error - Argument 2 passed to Symfony\\Component\\H ...

Investigate duplicate elements within nested arrays using the 3D array concept

I am dealing with an array that contains 3 subarrays. Arr = [[arr1],[arr2],[arr3]] My task is to identify and store the duplicate values found in these subarrays ([arr1],[arr2],[arr3]) into a separate array. Arr2 = [ Duplicate values of arr 1,2,,3 ] What ...

Incorporating an external library into a Node.js virtual machine

I'm currently working on a nodejs library that enables users to write and execute their own JS code. Here is an example: var MyJournal = Yurnell.newJournal(); module.exports = function(deployer) { MyJournal.description = "my first description& ...

Retrieve geographical coordinates from image metadata using JavaScript

Seeking help with extracting the GPS Exif tag from images using NodeJS. The data is currently structured as follows: { "gps": { "GPSTimeStamp": [2147483647, 76, 41], "GPSLongitude": [76, 41, 56.622], "GPSLatitude": [30, 43, 8 ...

Acquire parameters for the specific category and page

Utilizing this code snippet on my blog homepage enables pagination functionality. The pages are structured like url/?page=2 etc. export default async function HomePage({ searchParams, }: { searchParams: {[key: string]: string | string[] | undefined ...

Input values in Angular are not being updated according to the corresponding model values

My Angular application features two routes that share the same controller and partials. Despite the shared code, the controller and partials behave slightly differently depending on the route being used. Here are the routes: $routeProvider.when('/joi ...

Exploring the process of altering URLs in NextJS through rewrites

In my nextjs application, I am trying to implement a page redirection using the rewrites function in the configuration file next.config. Below is a snippet from my config file: const nextConfig = { reactStrictMode: true, async rewrites() { retu ...

Error encountered while attempting to render a form within a partial in Rails 5: "simple_fields_for" method is not defined for the SimpleForm::FormBuilder instance

This is a continuation from this thread: Passing a form as a local to a ajax rendered partial in Rails 5 I've searched extensively but haven't been able to find a working solution. Relevant Controller (profits_controller.rb): def new_tabs ...

Tips for saving and accessing the value of an md-select ng-model in an AngularJS dialog?

Currently, I am utilizing a template to populate an md-dialog box: The procedure for displaying the dialog: $scope.showDialog = function(element) { var parentEl = angular.element(document.body); $mdDialog.show({ template: element, scope: $scope, pr ...

Having Difficulty Configuring Async Request Outcome

I'm struggling to access the outcome of an asynchronous request made to an rss feed. After reading a post on How do I return the response from an asynchronous call?, which recommended using Promises, I tried implementing one. However, even though the ...

Angular template driven forms fail to bind to model data

In an attempt to connect the model in angular template-driven forms, I have created a model class and utilized it to fill the input field. HTML: <div class="form-group col-md-2 col-12" [class.text-danger]="nameCode.invalid && nameCode.touched ...

Could not locate module: The package path ./react is not exported from the package in E:NextAppportfolio_website-mainportfolio_website-main ode_modules ext-auth

I am encountering an issue while trying to import SessionProvider from Next-Auth. The error message that is being displayed is: "Module not found: Package path ./react is not exported from package E:\NextApp\portfolio_website-main\port ...

getting rid of the angular hash symbol and prefix from the anchor tag

I am having difficulty creating a direct anchor link to a website. Whenever I attempt to link to the ID using: where #20841 is my anchor tag. Angular interferes with the URL and changes it to: This functions properly in Chrome and Firefox, but i ...

Exploring JSON Array Data and Its Sub-Properties in React

I have a JSON array containing personal data that I want to access from my React app. This JSON file is already included in my React application. { "person": [ { "id": "userId", "sellerImage": "https://i.pravatar.cc/300", ...

Does anyone have any sample code on processing JSON data from a URL using Modified JavaScript Value in Java?

Could anyone provide some examples on how to handle returned Json data using Modified JavaScript Value? Here is the specific data I require from the json url: { "result": { "data": [ { "name": "page1", "period": "dia", "values": [ { "value": 4, "end_time" ...