Run the JavaScript configuration (Object) prior to converting it to JSON format

Is there a way to implement functions within a JavaScript file (configuration for backend server) that execute before parsing the data to JSON?

For example: config.js

module.exports = {
    structure_layout: {
        BUILDING: "BUILDING",
        FLOOR: "FLOOR",
        ROOM: "ROOM",
    },
    structure: {
        HOUSE: {
            type: function () {
                return this.structure_layout.BUILDING
            }
        },
        FLAT: {
            type: function () {
                return this.structure_layout.FLOOR
            }
        },
    }
};

The expected output in config.json after parsing should be:

{
  "structure_layout": {
    "BUILDING": "BUILDING",
    "FLOOR": "FLOOR",
    "ROOM": "ROOM"
  },
  "structure": {
    "HOUSE": {
      "type": "BUILDING"
    },
    "FLAT": {
      "type": "FLOOR"
    }
  }
}

Currently, when using JSON.stringify, I am not getting the desired results with empty type attributes.

Are there any alternative methods to define a JavaScript version of the config without relying on functions to achieve the same outcome?

Answer №1

To set up the configuration, you can run the code without using functions. Assign the desired values directly to the properties before exporting module.exports.

const settings = {
    layout_structure: {
        SCHOOL: "SCHOOL",
        GYM: "GYM",
        LAB: "LAB",
    },
    design: {}
};

settings.design.CLASSROOM = {type: settings.layout_structure.SCHOOL};
settings.design.GYMNASIUM = {type: settings.layout_structure.GYM};

module.exports = settings;

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

Steps to replace the content of an HTML file (such as modifying images) by clicking on an element in a separate HTML file

Currently, I am in the midst of a project and wondering if it is possible to dynamically modify the content of an HTML file, such as images and text, using JavaScript. My goal is to achieve this without relying on any frameworks, simply by clicking on el ...

I am encountering difficulties with a nodejs query where I am unable to successfully include the "+" symbol as part of the query

Every time I submit a query for B+ or A+ {{URL}}/api/help/?bloodType=B+ it ends up showing as empty space, like this. Is there a way to properly pass the "+" sign in the query? Thanks. P.S: _ works fine. {"bloodType":"B "} ...

The functions Show() and Hide() may not work in all scenarios within jQuery

I'm currently developing a website that allows users to participate in quizzes. Each quiz consists of 20 questions divided into three sections: 1 mark for 10 questions, 2 marks for 5 questions, and 4 marks for 5 questions. For each question, there are ...

Can JSON.parse be used on only a portion of an object in JavaScript?

Currently, I am facing an issue with a lengthy JSON file that I am fetching from a URL using https.request. Upon attempting to parse the string with JSON.parse, I encounter an "Unexpected end of JSON input" error. It appears that there is a limit to the ...

Clickability issue with searchbar results caused by onBlur event

My searchbar functionality includes showing results in a dropdown list when the user searches. However, I am facing an issue with the onBlur event that changes the display of the list to none when the user clicks away from the search box. The problem aris ...

Ensure that the dropdown menu remains visible even after the mouse no longer hovers over it

Looking to create a main menu with dropdown items that appear when the user hovers over them? You may have noticed that typically, the dropdown disappears once the hover event is lost. But what if you want the menu to stay visible and only disappear upon c ...

Whoops! The input buffer seems to be containing an image format that is not supported while attempting to utilize Next JS next-opt

I initially used the default Image Optimization component in my Next JS app, only to realize that the site could only be hosted on Vercel and not on other web hosting platforms. This limitation prompted me to explore the next-optimized-images package, whic ...

Is it possible to change the inner html to null during an active ajax request?

My goal is to have two separate data.html files inserted into two different HTML files without needing a click event. I want the structure of my data.html files to remain consistent, while allowing the template of my website to change dynamically by callin ...

Scala interprets JSON data in a condition

I am currently working with two classes that have a common parent in Scala. My goal is to implement a shared JSON reader within the parent class that can determine and return the appropriate child class based on the supplied JSON data. To better illustrate ...

Determining the starting and ending position of text within an element using jQuery

Delving into the world of jQuery/JS has been both challenging and exciting for me. However, I've encountered a problem that's got me stumped. Here is a snippet of text that I need to work with: blablablabla <b data-start="" data-end="">#fo ...

Tips on utilizing the identical template in ngIf

I need to display different templates based on certain conditions. For example: <template [ngIf]="item.url.indexOf('http') == -1"> <a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" > ...

JavaScript function to provide a range of numbers based on a specified number and step value

I am looking for a solution to dynamically generate content based on the number of slides/steps using JavaScript. Any suggestions on how to best achieve this? Thanks in advance! switch(this.currentSlide) { case 1: return '1-12'; ...

The Nuxt.js and Vue.js Watchers are caught in an infinite loop caused by fluctuations in the values

Currently, I am working on a project using Nuxt.js/Vue.js for creating an application that can efficiently convert XML to JSON and vice versa. Users have the ability to input values into textareas, which are managed by CodeMirror. The textarea is connecte ...

Headers cannot be set once they have already been sent in NodeJS

Here is the code where I authenticate users in a group, push accounts into an array, and save them using a POST request on /addaccount. groupRouter.post('/addaccount', Verify.verifyOrdinaryUser, function(req, res, next) { Groups.findById(req.bod ...

Feed information into a Select element from the Material UI version 1 beta

Having trouble populating data from a < Select > component in Material UI version 1.0.0.0 beta. Snippet of my code: This section is located inside the render() method. <Select value={this.state.DivisionState} onChange={this.handleChangeDi ...

json-server does not rely on the code found within the middleware file

While working on front-end development, I decided to use json-server to mimic some API calls. However, I encountered an issue with adding a middleware: { "name": "my-app", "version": "0.1.0", "private": true, "scripts": { "serve": "concurrentl ...

Similar to `util.inspect` in Node.js, Deno also has a function

Is there a utility function in Deno that can stringify an Object or primitive similar to Node.js util.inspect? For instance, if I have a JSON object in Node.js and want to display its contents: > m = {k1:'v1', k2:'v2'} { k1: ' ...

Switch Your PHP Script to Use PDO

Hello, I am new to PHP and seeking some guidance. My goal is to convert the following code to PDO in order to generate a JSON output for an Android app that I am currently working on. I have tried several solutions but encountered issues with the JSON resp ...

The issue of using an import statement outside a module arises when executing Protractor

I am facing an issue while running Protractor with my two files. When I execute the command "protractor protractor.config.js", I encounter the following error: D:\work\staru-app>protractor protractor.config.js [16:57:17] I/launcher - Running ...

Error message: Unable to split path as a function when utilizing React hook forms in conjunction with Material UI

Check out this code snippet: <TextField name="name" required className='my-2 mx-auto' label="Full Name" variant="standard" style={{ "width": "60%" }} value={name} onChange={(event) => { set ...