how to assign variables to an object for submitting the Data using reactjs?

Arunya

My objective is to assign the variables to new object while submitting the data.

Here is the array of object as they appear while submitting:

experience: [
    {
        Organisation: "",
        ID: "",
        From: "",
        To: "",
        Skills: []
    },
    {
        Organisation: "",
        ID: "",
        From: "",
        To: "",
        Skills: []
    }
]

But i want the object should be in this way:

{
        organisationName: "",
        id: "",
        from: "",
        to: "",
        skills: []
    }

I have done in this way but output is not appearing in this appropriately.

handleSubmit = (e)=> {
e.preventDefault();
let obj = {
id: this.state.experience.map((item) => item.ID),
organisation: this.state.experience.map((item) => item.Organisation),
        from: this.state.experience.map((item) => item.From),
        to: this.state.experience.map((item) => item.To),
}
console.log("Object", obj)
}

Can anyone please help me to assign the values in a right way?

CertainPerformance

It would be easier to use Object.fromEntries to map the entries and call toLowerCase on the keys:

const lowercaseKeys = obj => Object.fromEntries(
  Object.entries(obj)
    .map(([key, val]) => [key.toLowerCase(), val])
);
const obj = this.state.experience.map(lowercaseKeys);

If you also need to replace a particular key with another, use .replace:

const lowercaseKeys = obj => Object.fromEntries(
  Object.entries(obj)
    .map(([key, val]) => [
      key.toLowerCase().replace('organisation', 'organisationName'),
      val
    ])
);
const obj = this.state.experience.map(lowercaseKeys);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to assign data values in array using ReactJS

How to assign different variables to a class object using for loop?

How to assign many variables to an object?

How to pass a application variables through window object to console using reactjs?

How to assign columns of data to variables

How to assign forkJoin data to variables?

How to get data using two different state variables in ReactJS?

How can i redirect on submitting my form in reactjs and using formik

How to initialiaze an object and assign different variables javascript

How Read different data type in text file using python & assign them to variables using python

Request body is undefined when submitting Form Data object using supertest

Using variables as keys in Object supplied to Object.assign

How to split data and assign it into designated variables?

How to assign an object to another object using Scanner

PHP - How to assign object variables to function variables automatically?

How to assign variables using dynamic sql

How to assign tf.Variables using placeholders?

How to assign multiple DataFrame variables using a for loop?

How to assign an object's property to another one of it's properties in reactjs?

How to assign data from CSV into Object

How can i get data or pass a data from one page to another after submitting a form in reactjs

How to assign json object using getJSON

how to assign a key in object using javascript

How to add a 'data-*' attribute when using Object.assign to create an html element

How to assign data from a subcollection to its collection and render the new object? Using react, firebase and useeffect-hook

How to send data to servlet using ajax without a submitting form

Reading XML data using Powershell foreach and assign to the values Variables

Assign multiple variables that reference each other, using data.table

How to render object data to table with Reactjs