为键的每个值添加元素 - JSON
Add element for each value of the key - JSON
我有一个带有用户输入的表单,我在 JSON 中序列化了数据。我想要的是,对于指定键的每个值,我想在用户应该填写输入时添加一个前缀,比如如果用户输入 123,最终输出应该是 http://123
,但只针对一个输入字段,而不是我表单中的每个输入字段。
这是我的代码,在我执行控制台日志之前一切正常:
function convertFormToJSON(form) {
var array = jQuery(form).serializeArray();
var json = {};
jQuery.each(array, function() {
json[this.name] = this.value || '';
});
return json;
}
console.log(convertFormToJSON(document.getElementById("myform")));
var blah=convertFormToJSON(document.getElementById("myform")));
function addStringToValueInJSON(json, key, string) {
for (var i = 0; i < json.length; i++) {
json[i][key] = json[i][key] + string;
}
return json1;
}
function add() {
addStringToValueInJSON(blah, "test", "!");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myform">
<input type="text" id="test" name="test" value="2">
</form>
普通 JS 解决方案
const JSON = {}
const formData = new FormData(document.getElementById('myform'))
for (let [key, value] of formData.entries()) {
key += '_' // added as edit to answer at comment
JSON[key] = `http://${value}`
}
console.log(JSON)
<form id="myform">
<input type="text" id="test" name="test" value="2">
</form>
多值版本:
const JSON = {}
const formData = new FormData(document.getElementById('myform'))
for (let [key, value] of formData.entries()) {
key += '_' // added as edit to answer at comment
const vals = value.split(',')
const finalValsArray = vals.map(val => `http://${val}`)
JSON[key] = finalValsArray.toString()
}
console.log(JSON)
<form id="myform">
<input type="text" id="test" name="test" value="2,3,5">
</form>
我有一个带有用户输入的表单,我在 JSON 中序列化了数据。我想要的是,对于指定键的每个值,我想在用户应该填写输入时添加一个前缀,比如如果用户输入 123,最终输出应该是 http://123
,但只针对一个输入字段,而不是我表单中的每个输入字段。
这是我的代码,在我执行控制台日志之前一切正常:
function convertFormToJSON(form) {
var array = jQuery(form).serializeArray();
var json = {};
jQuery.each(array, function() {
json[this.name] = this.value || '';
});
return json;
}
console.log(convertFormToJSON(document.getElementById("myform")));
var blah=convertFormToJSON(document.getElementById("myform")));
function addStringToValueInJSON(json, key, string) {
for (var i = 0; i < json.length; i++) {
json[i][key] = json[i][key] + string;
}
return json1;
}
function add() {
addStringToValueInJSON(blah, "test", "!");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="myform">
<input type="text" id="test" name="test" value="2">
</form>
普通 JS 解决方案
const JSON = {}
const formData = new FormData(document.getElementById('myform'))
for (let [key, value] of formData.entries()) {
key += '_' // added as edit to answer at comment
JSON[key] = `http://${value}`
}
console.log(JSON)
<form id="myform">
<input type="text" id="test" name="test" value="2">
</form>
多值版本:
const JSON = {}
const formData = new FormData(document.getElementById('myform'))
for (let [key, value] of formData.entries()) {
key += '_' // added as edit to answer at comment
const vals = value.split(',')
const finalValsArray = vals.map(val => `http://${val}`)
JSON[key] = finalValsArray.toString()
}
console.log(JSON)
<form id="myform">
<input type="text" id="test" name="test" value="2,3,5">
</form>