I'm wondering why the keys in my string object for a select box are being transformed into numbers after deploying my Next.js application

Within my next.js application,

I have implemented an object with string keys and string values within a select box, as shown below:

export const HOURS: { [key: string]: string } = {
  '00': '00',
  '01': '01',
  '02': '02',
  '03': '03',
  '04': '04',
  '05': '05',
  '06': '06',
  '07': '07',
  '08': '08',
  '09': '09',
  '10': '10',
  '11': '11',
  '12': '12',
  '13': '13',
  '14': '14',
  '15': '15',
  '16': '16',
  '17': '17',
  '18': '18',
  '19': '19',
  '20': '20',
  '21': '21',
  '22': '22',
  '23': '23',
} as const

While testing locally, the object displays correctly as defined.

However, upon bundling and deploying the application, the keys are automatically converted to numbers, resulting in a different order for the select box ('00' becomes 0 and '01' becomes 1).

0: "00",
1: "01",
2: "02"
...

Is there a solution to maintain the original key declaration and preserve the desired order?

I have attempted specifying types { [key: string]: string } as well as using as const, but these methods have not been successful.

Answer №1

According to Denris mentioning, the reason for this is the swc config option, which can be disabled in next.config.js by setting: swcMinify: false,

Answer №2

It's important not to rely on the sorting order of objects, as it is not guaranteed. A more reliable solution would be to use Map instead.

If we delve deeper: Based on your object being named HOURS, it seems like you may be working with time data. If the main purpose is to add a leading zero to single digit values, there are various methods to achieve this depending on your specific implementation and date formatting requirements. You can explore alternatives such as changing getHours to 2 digits in JavaScript, or following a simple approach like this one for getting minutes with leading zeros in JavaScript.

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

Response from Node ACS returning a 404 Not Found status code to a Java HTTP client

Currently, I am working on a project where there is a Java scheduler acting as an HTTP Client sending XML data to my node application. Within the index function of application.js, I have implemented the logic to receive the XML and convert it into JSON. Ho ...

A guide on incorporating JavaScript variables within a GraphQL-tag mutation

I'm having trouble consistently using javascript variables inside graphql-tag queries and mutations when setting up an apollo server. Here's a specific issue I've encountered: gql` mutation SetDeviceFirebaseToken { SetDeviceFirebaseTok ...

Is it recommended to place the styles created by material-ui for child components after the styles generated for the parent component?

Utilizing material-ui, which utilizes JSS for styling a website. I have a component named Layout that applies margin to all its children (using the all children selector & > * in its style). This functionality works, but I would also like the child ...

Unable to use jQuery to choose an item from a dropdown menu and reveal a hidden text box

Seeking assistance in displaying a paragraph with text and a textbox when a specific option is selected from the dropdown menu on my form. Previously used code for radio buttons, but encountering issues with this scenario. Any guidance would be greatly app ...

Strategies for maintaining pristine Firebase child paths

I have a list of data that I want to structure in Firebase. However, I encountered an error: Error: Firebase.child failed: The first argument provided is not a valid path. Path names should not include ".", "#", "$", "[", or "]" characters and must be no ...

What is the best way to implement the navbar toggler feature in a Bootstrap dashboard?

Currently, I am in the process of developing an Admin Panel using the Bootstrap Material Dashboard provided by Creative Tim. Although everything has been set up correctly, there seems to be an issue when viewing the panel on smaller screens. The navbar to ...

How do I resolve the issue of the `translate` array in `setup()` in `vuei18n` requiring an additional step or load to render the translations in the `<template/>`?

Apologies for the mention of the "extra step/load" aspect; I acknowledge that it may come across as vague and not the best choice of words. Allow me to illustrate with code snippets and two accompanying images. 1st: https://i.sstatic.net/QtDXD.png Here is ...

Headers cannot be sent to the client after they have already been set in Axios within Next.js

For additional discussion on this issue, please refer to the GitHub thread at - https://github.com/axios/axios/issues/2743 In my Next.js project, I am using Axios and occasionally encounter an error related to interceptors when returning a Promise.reject. ...

Tips for disregarding single quotation marks in the validator.js function for checking alphanumeric characters

I am currently working on validating a string using express validator, specifically utilizing the isAlphanumeric function. However, I would like to include space, dash, and single quote as acceptable characters in the validation process. Below is the code ...

What is the best way to run a function after 10 seconds of inactivity using jquery?

Can anyone help me figure out how to run a function every 10 seconds when the system is idle? Here's an example: setInterval(function () { test(); },10000); //for every 10 Sec I really need assistance in getting this function to ...

Leveraging the package.json file for client-side packages, enabling them to be dynamically loaded in the browser

Considering expanding the structure of package.json to incorporate dynamic package (plugin) loading on the client side. I am curious about how this idea aligns with the vision of npm. Essentially, I am interested in loading multiple modules with shared met ...

How can I use regex to filter out specific string values?

I am new to the world of regex. I have a string that looks like this: cn=foo,ou=bar,ou=zoo,ou=aa,ou=bb,ou=cc,ou=dd,o=someOrg,c=UK. My goal is to extract foo, bar, and zoo from it using JavaScript regex. const dn = 'cn=foo,ou=bar,ou=zoo,ou=aa,ou=bb,ou ...

The Bootstrap Navbar appears hidden beneath other elements on mobile devices

While using bootstrap to style my header contents, I encountered a strange issue. The navbar that appears after clicking on the hamburger menu is displaying behind all the components. Even though I've set the z-index to the maximum value, it still doe ...

Creating a dynamic and interactive menu using three.js

In a team project, one member has utilized three.js to create the graphical elements of our software, while I have been assigned the task of developing a Menu / Display feature within the program due to time constraints. The software showcases a 3-D graph ...

Unexpected output from nested loop implementation

Having some arrays, I am now trying to iterate through all tab names and exclude the values present in the exclusion list. json1 ={ "sku Brand": "abc", "strngth": "ALL", "area ...

Using Angular $resource to store an object with arrays

Consider a scenario where we have a User $resource structured as follows: $scope.user = { name: 'John', hobbies: [1, 2, 3] } If we were to save User.save($scope.user) to the server, it would send the following parameters: name: 'John& ...

Tips on sending access token and store name to a new object in Shopify API Node

In the process of developing a public Shopify app, I am looking to implement a POST route for creating a metafield. According to the documentation found in the shopify-api-node module, it states: accessToken - Necessary for public apps - This is a string ...

Unexpected rejection of a promise, despite my confidence in having addressed all of them

I have created a node application that retrieves data from an API using various endpoints. To achieve this, I am utilizing proxies. Currently, I am employing socks-proxy-agent to establish an https agent for my axios instances in order to use the proxy. ...

Storing information from a form into a database with the help of TypeORM on Angular 6

Utilizing TypeORM alongside Angular to store form data in the database has been successful. The connection configuration is correct, allowing for data storage from the backend. { "type": "mssql", "host": "***", ...

Retrieve calling image from array on rollover

Currently working on a website where I want to incorporate multiple rollovers using the same base image (distributed black pixels all over the page to create a popup effect). I decided that the best way to approach this is to use an array with all the ro ...