What steps should I take to maximize the efficiency of my angular function?

Hey there, I could really use some assistance with optimizing this code snippet. Does anyone have any ideas on how to improve it? Here's the code:

optimizeCode(value, fieldName: string) {
if (fieldName === 'fullName') {
  this.billingFields.full_name = value;
}
if (fieldName === 'address') {
  this.billingFields.address = value;
}
if (fieldName === 'postalCode') {
  this.billingFields.postal_code = value;
}
if (fieldName === 'city') {
  this.billingFields.city = value;
}
if (fieldName === 'stateOrProvince') {
  this.billingFields.state_or_province = value;
}
if (fieldName === 'taxId') {
  this.billingFields.tax_id = value;
}

}

Answer №1

One way to simplify mapping field name values to object key names is by creating a custom object for this purpose.

const fieldMap = {
  fullName: 'full_name',
  address: 'address',
  postalCode: 'postal_code',
  city: 'city',
  stateOrProvince: 'state_or_province',
  taxId: 'tax_id'
}


updateFieldInfo(value, fieldName: string) {
   if (fieldMap[fieldName]) {
      this.billingFields[fieldMap[fieldName]] = value;
   }
}

Answer №2

Mapping Version

const mapping = new Map([
   ['fullName', 'full_name'],
  ['address', 'address'],
  ['postalCode', 'postal_code'],
  ['city', 'city'],
  ['stateOrProvince', 'state_or_province'],
  ['taxId', 'tax_id']
]);

handleChangeValue(value, fieldName: string) {
   if (mapping.get(fieldName)) {
      this.billingFields[mapping.get(fieldName)] = value;
   }
}

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

Using Firefox to cache Ajax calls after navigating back in the browsing history

Currently, I am utilizing an ajax call to invoke a php script that waits for 40 seconds using sleep and then generates the output RELOAD. Subsequently, in JavaScript, the generated output is validated to be RELOAD, following which the call commences again. ...

Using Javascript regex to remove spaces inside parentheses

Can you provide the specific Javascript regex code for removing spaces only within parentheses? Take for instance: balance( 11010000 ) / balance( 11050000 ) The desired output should be: balance(11010000) / balance(11050000) ...

Incorporating AJAX functionality into an existing PHP form

I am currently working on a PHP registration form that validates user inputs using $_POST[] requests. Validating username length (3-20 characters) Checking username availability Ensuring the username matches /^[A-Za-z0-9_]+$/ pattern and more... Instead ...

A guide on incorporating java.type into your JavaScript code

I've been attempting to utilize the following command in my JavaScript: var file = new Java.type("java.io.File"); Unfortunately, I'm encountering an error: Uncaught ReferenceError: Java is not defined Can anyone provide guidance on how to suc ...

Angular is encountering an issue where it is unable to access a property of undefined on an

There appears to be a puzzling issue in this code snippet. Despite my best efforts, it keeps throwing an error that I can't seem to pinpoint. Perhaps someone with a fresh perspective can spot the problem... The class is lengthy, but I felt it was nec ...

Numerous buttons activating a single modal component

I have an array called videos, which contains objects of the type Video (defined below). My goal is to create a functionality where clicking on a specific video button opens a modal containing information about that particular video. interface VideosInfo ...

Implement a universal JavaScript UI library into an Angular application

For my Angular application (Angular4.x), I am using the mapwize sdk which is a pure javascript library. I have successfully displayed a MapWize map by including the necessary scripts and styles in the index.html file, as shown below: <head> ...

Why can't I capture the text within this particular div using .text?

Trying to extract specific text from a website in Chrome's developer console. For example, here is the code snippet: <div class="someClass">This is some text!</div> Expected it to work with this command, but it returns 'undefined&a ...

Showcase your skills in CSS, HTML, and Javascript

I attempted to search for something similar but came up empty-handed. I have two buttons. When I click one of them, I want to hide a certain division, but it's not working as expected. The divs d2 and d3 are initially hidden when the page opens, whic ...

The Javascript function call from the <img src="myFunction()"> is malfunctioning

I am looking to dynamically pass the URL of an image using a JavaScript function. <head> <script> function myFunction() { var str1 = "somepictureurl.png"; return str1; } </script> </head> <body> <img src="myFu ...

Warning: Unidentified JavaScript alert(notification) encountered without declaring a

Imagine this scenario - when I type the following command: open google.com I need JavaScript to detect "open google.com" and prompt me with an alert. The challenge is figuring out how to generate an alert for just "google.com" without including "open". ...

Is there a way to ensure that all asynchronous functions have finished executing before assigning them to module.exports?

Currently, I am working on developing an app that generates routes based on data retrieved from a MongoDB database using Mongoose. Here is the current setup: var app = express(); var articleRoute = require('./article.js'); var Articles = requi ...

In order to launch an Angular project

Currently, I am in the process of creating a VSS web extension using Angular... To generate a .vsix file, I need to reference an HTML file. The challenge arises when working with Angular because we typically use ng serve which loads our page at http://lo ...

Information vanishes as the element undergoes modifications

I am currently working with a JSON file that contains information about various events, which I am displaying on a calendar. Whenever an event is scheduled for a particular day, I dynamically add a div element to indicate the presence of an event on the c ...

Angular component is not identifiable within the Angular framework

Here's the content of my todos.component.ts file: import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-todos', standalone: true, imports: [], templateUrl: './todos.component.html', sty ...

Developing a type specifically for this function to operate on tuples of varying lengths

I am currently developing a parser combinator library and I am in need of creating a map function that can take N parsers in a tuple, along with a function that operates on those N arguments and returns a parser that parses into the return type specified b ...

Why is the JavaScript code not functioning when the page loads?

Here is the HTML code snippet: <head> <link href="/prop-view.css" media="screen" rel="stylesheet" type="text/css"> <script src="/javascripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script type="tex ...

Adjusting webpage background with JavaScript

I've been struggling with this for the past six hours and can't seem to get it right. I've checked out various solutions on Stack Overflow, but nothing seems to work. What am I missing here?!? My html5 page doesn't have a background an ...

Leveraging Bootstrap grid system within AngularJS elements

I am currently working on wrapping grid element divs into Angular components in order to streamline the input process and establish a standard: <bootstrap-row> <bootstrap-input-text col=6 ng-model="$ctrl.model" label="hey!"& ...

Enhancing user experience by implementing AJAX in a contact form to eliminate the need for page

I have read numerous questions on this topic and have compiled the code I currently have from various responses. Unfortunately, despite my efforts, I am unable to make it work and I cannot identify the reason behind this issue. Below is the HTML form str ...