Could the selection determine if a value is exported?

Need help with updating a Typescript code snippet:

export const defaultListingFormValues = {
  itemWeight: 1
}

Is it possible to dynamically adjust the default value of itemWeight based on a selected category in a dropdown menu for a listing form?

For example, setting itemWeight to 0.5 for "dress", 1 for "heels", 2 for "boots", etc., depending on the chosen option?

Answer №1

To establish the values for your defaultListingFormValues including categories such as objects, you can use the structure

{ itemWeight: 1, dress: { itemWeight: 1 },... }
.

Utilize these values based on the selected category from a dropdown menu. If the categoryFromDropDown references a category like dress, the code will fallback to a default value if the category is not explicitly defined.

let defaultValue = defaultListingFormValues[categoryFromDropDown] ?
      defaultListingFormValues[categoryFromDropDown].itemWeight :
      defaultListingFormValues.itemWeight;

Experiment with this concept below. The usage of export & import statements are included as comments.

// export const defaultListingFormValues = {
const defaultListingFormValues = {
  itemWeight: 1,
  dress: {
    itemWeight: 0.5
  },
  heels: {
    itemWeight: 1
  },
  boots: {
    itemWeight: 2
  }
}

// When you access 
// import defaultListingFormValues from '...';
let categoryFromDropDown = 'dress';
let defaultValue = defaultListingFormValues[categoryFromDropDown] ?
  defaultListingFormValues[categoryFromDropDown].itemWeight :
  defaultListingFormValues.itemWeight;
  
console.log(defaultValue);

// category which is not present in defaultListingFormValues.
categoryFromDropDown = 'aaa';
defaultValue = defaultListingFormValues[categoryFromDropDown] ?
  defaultListingFormValues[categoryFromDropDown].itemWeight :
  defaultListingFormValues.itemWeight;
  
console.log(defaultValue);

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

Enhancing Angular Material: requiring more user engagement for rendering to occur

Encountering an unusual issue with Angular Material: certain components require an additional event, like a click or mouse movement on the targeted div, to trigger the actual rendering process. For instance, when loading new rows in mat-table, some empty ...

The 'IncomingHttpHeaders' type cannot be assigned to the 'BusboyHeaders' type

I am currently using the busboy module in my TypeScript/Node project for file uploading. In all the documentation for busboy, they suggest initializing it with request headers. However, I am encountering the following error: Type 'IncomingHttpHeaders& ...

Encountered a problem with regular expressions in Angular 2 - a Module parse error due to an octal literal in strict mode

Greetings, I have encountered an issue with a regular expression in my environment.ts file. export const environment = { passwordPolicy: "^(?!.*(.)\1\1)(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}.*$" }; Unfortunately, whe ...

Guide on storing geolocation information in an array on Google Firebase Realtime Database using Angular HttpClient

I have been working on developing an innovative Android geolocation tracking app using Ionic with the assistance of the Cordova Geolocation plugin. The tracking feature has been successfully implemented thus far. However, I am currently facing challenges w ...

Retrieve the HTML data from a form created with Angular

Here is an example of an Angular form: <div #myForm [formGroup]="myForm"> <select formControlName="productName" class="form-control"> <option value="">Select</option&g ...

Change object values to capital letters

Upon retrieving myObject with JSON.stringify, I am now looking to convert all the values (excluding keys) to uppercase. In TypeScript, what would be the best way to accomplish this? myObj = { "name":"John", "age":30, "cars": [ { "name":"Ford", ...

Troubleshooting typescript error in styled-components related to Material-UI component

When using typescript and trying to style Material UI components with styled-components, encountering a type error with StyledComponent displaying Type '{ children: string; }' is missing the following properties import React, { PureComponent } f ...

Executing the ngIf directive in Angular 2 when a function or click event occurs

Is there a way to display an element only when a specific function is executed or a particular click event occurs? Here's the html code snippet I'm currently working with: <sebm-google-map [latitude]="lat" [longitude]="lng" [zoom]="zoom" [map ...

Issue [ERR_MODULE_NOT_FOUND]: The module 'buildapp' could not be located within buildserver.js

I am currently working on a node project using typescript. The project's structure is organized in the following way: --src |-- server.ts |-- app.ts --build |-- server.js |-- app.js In server.ts: import { app } from &q ...

Service consuming in Angular 2 using Stomp protocol

Why am I seeing responseBody as undefined, but I am able to see the subscribe response in msg_body? What could be causing this issue with responseBody? let stomp_subscription = this._stompService.subscribe('/topic/queue'); stomp_subscription.ma ...

Is it possible to interchange the positions of two components in a routing system?

driver-details.component.ts @Component({ selector: 'app-driver-details', templateUrl: './driver-details.component.html', styleUrls: ['./driver-details.component.css'] }) export class DriverDetailsComponent implements OnI ...

Encountering an issue with the form onSubmit function in React TypeScript due to an incorrect type declaration

I have a function below that needs to be passed into another component with the correct type definition for updateChannelInfo, but I am encountering an issue with the type showing incorrectly: const updateChannelInfo = (event: React.FormEvent<HTMLFormEl ...

Tips for relocating the indicators of a react-material-ui-carousel

I am working with a carousel and dots indicators, but I want to move the indicators from the bottom to the circular position as shown in the image below. I attempted using a negative margin-top, but the indicators ended up being hidden. Is there another ...

Tips for assigning a preset value to a checkbox

Is there a simple way to set the default Checked value of this angular checkbox? <p class="card-text">Please let me know which public organizations have been notified.</p> <div for="stakeholders" class="custom-control custom-c ...

Ways to bypass HostListener in Angular 2

In the process of developing a page with animations triggered on scroll, I encountered the challenge of ensuring that the animations only occur when the corresponding element is visible on the viewport. Utilizing Angular2 for this task led me to investigat ...

Solving TypeScript Error in React for State Destructuring

Recently, I've been working on creating a React component for AutoComplete based on a tutorial. In my development process, I am using Typescript. However, I encountered an issue while trying to destructure the state within the render suggestions metho ...

Unlocking the Power of FusionAuth in NativeScript: A Guide

While attempting to utilize a library based on nativescript documentation, I encountered an issue where certain modules like net and tls were not being automatically discovered. After using npm install to include tls and net, the problem persisted with t ...

How come JSON.parse is altering the data within nested arrays?

In my journey to master Angular 2, I decided to challenge myself by creating a Connect Four game using Angular CLI back when it was still utilizing SystemJS. Now, with the switch to the new Webpack-based CLI, I am encountering a peculiar issue... The fun ...

Extract the checkbox value within a table

I'm facing a challenge with a table that has multiple columns and a checkbox for one of the columns. Table layout: https://i.sstatic.net/qKZHt.png I attempted to assign some formControlName to the checkbox within the table, but I received an error ...

Creating a task management application using Vue 3 Composition API and Typescript with reactivity

I am in the process of creating a simple todo list application using Vue 3 Composition API and TypeScript. Initially, I set up the function for my component to utilize the ref method to manage the reactivity of user input inserted into the listItems array. ...