简体   繁体   English

如何在使用reactjs提交数据时将数字更改为字符串

[英]how to change number to string while submitting the data using reactjs

My objective is to change the value of variable while submitting the data.我的目标是在提交数据时更改变量的值。

Here is the array:这是数组:

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

I have tried changing in Onchange by giving value.toString() , but while submitting it is taking 0 if we dont give any value.我尝试通过提供value.toString()在 Onchange 中进行更改,但是如果我们不提供任何值,则在提交时将取0

So, thought to change the value while submitting the data.所以,想在提交数据时更改值。

Here is the handleSubmit() change:这是 handleSubmit() 更改:

handleSubmit = (e)=> {
e.preventDefault();
const lowercaseKeys = obj => Object.fromEntries(
  Object.entries(obj)
    .map(([key, val]) => [
      key.toLowerCase().replace('organisation', 'organisationName'),
      val
    ])
);
const obj = this.state.experiences.map(lowercaseKeys);
console.log("Object:", obj)
}

i'm not getting any idea how to change the value to string in the above object obj .我不知道如何将上述对象obj的值更改为字符串。

Can anyone help me in this query?任何人都可以帮助我进行此查询吗?

This may not be the perfect solution but maybe worth giving a go.这可能不是完美的解决方案,但也许值得一试。

Try using a template literal as in...尝试使用模板文字,如...

let myNewStr = `${numToBeConverted}`

Then save myNewStr into the DB instead of the string.然后将myNewStr保存到数据库而不是字符串中。

You can also do ${numToBeConverted} by itself.你也可以自己做${numToBeConverted}

For example: Instead of doing value.toString() you'd do例如:而不是做value.toString()你会做

`${value}`

Try changing val to ${val}尝试将 val 更改为${val}

Have you tried using a template literal?您是否尝试过使用模板文字? – Mason Curtis – 梅森柯蒂斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM