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

Specify a preset selection time in a TextField of time type in Material UI

I am looking to establish a default time for a textfield of type time in Material UI. My requirement is that before the user clicks on the picker, no time should be pre-set, but once they click, 08:00 should appear as the default time to choose from. View ...

Material-inspired Design Device Compatible DIV slide with JS, JQuery, and CSS

My goal is to achieve something similar to this: Desired Live Website I am looking for a feature where clicking on the div will slide in content from the right, load an external page inside it, and close when prompted. The slider div should be device c ...

Troubleshooting: Directives in Angular 4 not recognizing RegEx patterns

I have developed a directive that validates the input in a text field, allowing users to enter numbers, dots, and commas. However, the validator only seems to work for numbers and not for commas and dots. import { Directive, ElementRef, HostListener } fro ...

ways to assign local file path as image url in json

Hey there, I am a new tool for angularjs. Take a look at the json file below: [ { "name": "WORLD", "population": 6916183000, "flagurl":"C:\xampp\htdocs\selva \Flag_of_the_People's_Republic_of_China.svg" }, { "na ...

Encountering a "Element is not defined" error in Nuxt when trying to render Editor.js and receiving

I've been working on creating an editor using Editor.js within my Nuxt project, but it seems like the editor isn't initializing properly when I render the page. import EditorJS from '@editorjs/editorjs'; interface IEditor { editor: E ...

Send form data without reloading the page and connect it to a JavaScript script

I've designed a system that reveals values based on a specific input selection. Below is the main form where users can enter model numbers and press enter: <form> <input type="text" name="ModNum" id="ModelNumber" pattern="^PIV13RT[23]?$" ...

How to implement hover effect to show child div within parent div using React.js

Hey there! I'm fairly new to working with react js and currently trying to add some animation to a nested div. The parent div, known as " portfolio-product-item ", showcases a featured image pulled from the WP REST API. Inside this parent div, there&a ...

React - The content of my JSON.String vanishes upon being placed inside a div element

My NFTDetails component includes a description from a JSON, which contains \n\n in it. Strangely, there are no new lines when I render the JSON value in a div, but when I log it to the console in Firefox, new lines appear. How can I make use of ...

Ways of accessing an array within an if statement

I have a dashboard with admin privileges for my application. In this dashboard, the admin can select a user from a dropdown list. Once a user is selected, I make an AJAX call to retrieve the user's data and store it in a variable named $result. Howeve ...

Instructions for implementing this script in HTML and JavaScript: utilize the clone() function

I came across this code snippet here function displaytickets(){ var $panel = $('<div/>').addClass('col-xs-3 panel panel-default') $panel.append($('<div><h3 class="panel-title">Title</h3></div>&a ...

Is there a way to prevent the "undefined" message from displaying in the console when this code is run?

Help needed! Can someone assist me in resolving the issue where the word 'undefined' is being displayed in the console? I'm a beginner in programming and struggling with this. Here's what I'm currently seeing: You are not getti ...

When trying to bind an object that is constantly changing, one-way binding may not effectively capture those dynamic modifications

For a detailed review of the code, please check out the plnkr. I am quite new to AngularJS components. I have created two simple AngularJS components with the exact same bindings: bindings: { value:'@', field:'@', object: '<&a ...

Vuejs Error: "No template or render function defined for a single file component"

When attempting to import all components from a folder and display one based on a passed prop, I encountered an error at runtime. I am using webpack with vue-loader to import all my components, each of which is a *.vue file. The issue arises when importi ...

The select box in Material UI is not displaying the data as expected

I'm currently tackling an issue where, upon every click of an icon, a select box (from material ui) needs to be displayed with a few options inside it. The functionality should show the select box each time the icon is clicked. Here's a brief sum ...

Retrieving rows from a MySQL table that contain a specified BIGINT from an array parameter

I've encountered a problem with mysql while using serverless-mysql in TypeScript. It seems like my query might be incorrect. Here is how I am constructing the query: export default async function ExcuteQuery(query: any, values: any) { try { ...

Determine the hour difference between two provided dates by utilizing the date-fns library

My API returns a "datePublished" timestamp like this: "2019-11-14T14:54:00.0000000Z". I am attempting to calculate the difference in hours between this timestamp and the current time using date.now() or new Date(). I am utilizing the date-fns v2 library fo ...

Guide on extracting unique identifiers from an array of objects and sorting them by the earliest date in JavaScript

I've got an array of objects and I'm looking to retrieve the items with unique IDs while also selecting the earliest date. For example: [{id:1, date: Jan 12}, {id:2, date: Feb 8}, {id:3, date: Feb 8}] var array = [{id: 1, date: Jan 12 2021 08:00 ...

Issue with PHP Upload: Index is undefined

Having Trouble with PHP Upload: Encountered an issue: Undefined index: file in Error on line: $count = count($_FILES['file']['name']); Tried numerous codes to fix this error, but none have worked so far PHP Code: <?php $count ...

Locate the present position of the slider element

I am currently in the process of creating a website that features pages displayed in a slider format. Each page has its own unique ID, but all share a common class called 'section'. However, I am encountering difficulty in identifying the ID of t ...

React Component: Issue with toggle functionality in child component not meeting expectations

I'm currently working on a checkbox user interface, where I need to pass a toggle function down to a list component. The issue I'm facing is that while I can check the checkbox successfully, I'm unable to uncheck it for some reason. Below i ...