Use a function on values that have corresponding keys in a separate array

I am working with a form.value object that looks like this:

 form.value = {
  "to_date": "2019-03-21T05:00:00.000Z",
  "from_date": "2019-03-13T05:00:00.000Z",
  "is_form": ""
  "errors":""
}

Additionally, I have an array called filterArray:

filterArray = [
  "from_date",
  "to_date"
]

The goal is to loop through the keys in the form.value object and apply a function (converFormat()) to the values of keys that match the ones in the filterArray:

form.value = {
  "to_date": "2019-03-21T05:00:00.000Z",       // function() applied since key in filterArray  
  "from_date": "2019-03-13T05:00:00.000Z",     // function() applied since key in filterArray  
  "is_form": ""                               
  "errors":""                                  
}

Answer №1

Transforming selected keys in a form object using a custom function:

Object.keys(formData).filter(key => filterKeys.includes(key)).forEach(key => {
  formData[key] = transformData(formData[key])
})

// Alternatively, you can store the transformed values in another variable

const customizedFormData = {
  ...formData,
  ...Object.keys(formData)
    .filter(key => filterKeys.includes(key))
    .reduce(
      (previousVal, currentKey) => ({
        ...previousVal,
        [currentKey]: transformData(formData[currentKey])
      }),
      {}
    )
};

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

Guide to generating TypeScript output files within a non-hierarchical directory layout

In my project, I have a directory structure with multiple typescript files organized as follows: | src | app-1 | tsconfig.json | app-2 | tsconfig.json | common | standalone | tsconfig.json For each of the ...

SonarLint versus SonarTS: A Comparison of Code Quality Tools

I'm feeling pretty lost when it comes to understanding the difference between SonarLint and SonarTS. I've been using SonarLint in Visual Studio, but now my client wants me to switch to the SonarTS plugin. SonarLint is for analyzing overall pr ...

What is the best way to retrieve the dataset object from a chart object using chart.js in typescript?

Currently, I am facing a challenge in creating a new custom plugin for chart.js. Specifically, I am encountering a type error while attempting to retrieve the dataset option from the chart object. Below is the code snippet of the plugin: const gaugeNeedle ...

Incorporating Scatter Dots into a Horizontal Stacked Bar Chart using Chart.js

My horizontal stacked bar chart is not displaying pink scatter dots based on the right y axis numbers. I need help figuring out what I am doing wrong. When I change the chart to a normal bar instead of horizontal, the dots appear. However, I require them ...

Is it considered poor practice in TypeScript to manually set the type when the type inference is already accurate?

Is it necessary to explicitly set the variable type in TypeScript when it is inferred correctly? For example: const add = (a: number, b: number) => a + b; const result = add(2, 3); // Or should I explicitly declare the return value type? const add = ...

The inner panel height does not extend to 100% when there is overflow

When pressing the submit button on a panel containing components, an overlay appears but does not cover the entire parent panel if scrolled to the bottom. Additionally, I want the spinner to always be centered, whether scrolling or not. I've tried usi ...

Mismatched URLSearchParam type {property: value} does not match with undefined and Argument Type {property: value} cannot be assigned to {property: value}

Every time I try to run the code below, I encounter this error. The code is supposed to take a refresh token as input and make a POST request using URLSearchParams to a specific URL. However, I keep getting an error related to URLSearchParams. https://i.s ...

Development occurring concurrently within a single Angular project

Just getting started with Angular and gearing up to collaborate on an Angular project with a team of developers. I'm looking for advice on how we can effectively share our work on the Angular development project in real-time, essentially creating a c ...

Joi has decided against incorporating custom operators into their extended features

I am having trouble extending the joi class with custom operators. My goal is to validate MongoDB Ids, but when I try to use the extended object, I encounter the following error: error: uncaughtException: JoiObj.string(...).objectId is not a function TypeE ...

Creating a factory function through typhography

I have a dynamically generated list of functions that take an argument and return different values: actions: [ param => ({name: param, value: 2}), param => ({label: param, quantity: 4}), ] Now I am looking to create a function that will gen ...

Manipulate and send back information from Observable in Angular

Here is an example of a scenario where I have created a service to retrieve a list of properties. Within this service, I am utilizing the map function to manipulate the response and then returning the final array as an Observable. My question is: How can ...

Learn how to store the data from a FormArray into a MySQL database using Node.js

I am facing an issue and need assistance. Apologies for my limited English proficiency, but I will do my best to articulate the problem in hopes that you can assist me. Thank you. I am developing an activity management platform where employees log in ...

Angular 2 does not recognize the existence of .then in type void

I have a query regarding Angular2 and I'm struggling with the void function in my code. Can someone help me out? I am new to Angular2 and unsure of what needs to be added in the void function. Check out this image for reference export class PasswordR ...

Guide on using axios in vue.js to interact with the API

I need help with a functionality on my website where users can select a car brand from a list of radio buttons. After selecting a brand, I want to retrieve an array of models from the server and display them on a new page. Can someone guide me on what spec ...

Sort attributes by the type of property

Is there a way to create a customized type by extracting specific properties from a generic type? class Test { value1!: Date value2!: number value3!: Date value4!: string } type FilterProperties<T, TFieldType> = //looking for a solution to se ...

The Material Table in Angular is having issues with sorting functionality

I tried implementing the basic example from the angular material website, which displays a table with accurate data but the sorting functionality is not working as expected. For reference, you can view the StackBlitz demo here: https://stackblitz.com/edit ...

React-dnd supporting multiple draggable and droppable elements

I am facing a challenge with making multiple elements draggable using react-dnd. I have an array of 4 fields that I would like to make draggable, but when I map through the array and give each element a className of 'element', they do not move as ...

The complete Angular 2 application fails to load when accessed using a custom domain name

I've been struggling for the past few days trying to solve a strange issue. When I try to access my Angular 2 app using a domain name (example.com), it gets stuck on the loading screen. However, if I try accessing the same app without nginx, it loads ...

Searching for data in a JSON file and retrieving values from an array using Python

Looking at the JSON provided below, my goal is to search each array and extract data only from the ones that have keys and values in the "source" block. Arrays with an empty "source" block should be disregarded. Here's the JSON structure: { "L": [ ...

Exploring table iteration in Angular 7

I am looking to create a table with one property per cell, but I want each row to contain 4 cells before moving on to the next row... This is what I want: <table> <tr> <td> <mat-checkbox>1</mat-checkbox& ...