简体   繁体   English

如何将表单中的对象数据置于特定属性下

[英]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.

相关问题 如何将表单数据放入javascript中的对象中? - How to put form data in an object in javascript? 如何将表单数据序列化为具有嵌套属性的对象 - How to serialize form data into an object with nested properties 如何从已从 firebase 检索的表中获取特定数据值并放入表单 - How to get the specific data value from the table that has retrieve from firebase and put inside a form 如何从 JavaScript 中的 object 中省略特定属性 - How to omit specific properties from an object in JavaScript 如何实现复选框的属性以在不使用Grid对象或jQuery.Data(Input)的情况下跟踪表中每一行的更改? - How to implement properties of checkbox to keep track of your changes on each line in your table without employing Grid object or jQuery.Data(Input)? 如何将动态数据放在对象数组下? - How to put dynamic data under a array of objects? 如何从脏形式的 object 中获取值并将其放入数组中? - How to get values from object of dirty form and put into Array instead? 如何将特定属性从一个 object 分配给另一个 - How to assign specific properties from one object to another 如何从嵌套对象数组中保留特定对象属性 - How to keep specific object properties from an array of nested objects 如何使用javascript从每个JSON对象中提取特定属性 - How to extract specific properties from each JSON object using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM