[英]How to put your object data from a form under specific properties
I have a JSON getting submitted by a form eg below. 我有一个JSON,例如下面的表单提交。
Var obj1 = {
input1: 'name',
input2: 'surname',
input3: 'email'
}
Now the back-end database has been configured to accept values like this. 现在,后端数据库已配置为接受这样的值。
FormData: [{
"Key": "input1",
"Value": "Test"
}]
So each value needs to be under key and value, how do I put my input1 and input2 etc under a key and value property for every value in JavaScript? 因此,每个值都必须在键和值下,对于JavaScript中的每个值,如何将我的input1和input2等放在键和值属性下? I'm using React but plain JavaScript will do. 我正在使用React,但是普通的JavaScript可以。
var obj1 = { input1: 'name', input2: 'surname', input3: 'email' } console.log(Object.keys(obj1).map(Key => ({ Key, Value: obj1[Key] })))
Get keys of the object obj1
, and then map over it to produce an array of objects. 获取对象obj1
键,然后在其上映射以生成对象数组。
Try this : 尝试这个 :
let FormData = Object.keys(obj1).map(key => {
return {"Key" : key, "Value" : obj1[key]}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.