[英]JavaScript convert object with dynamic property length to array
I have a form (fields are created dynamically with javascript)我有一个表单(字段是用 javascript 动态创建的)
<input type="text" name="item[0][name]" />
<input type="text" name="item[0][email]" />
<input type="text" name="item[0][address]" />
<input type="text" name="item[1][name]" />
<input type="text" name="item[1][email]" />
<input type="text" name="item[1][address]" />
<input type="text" name="item[2][name]" />
<input type="text" name="item[2][email]" />
<input type="text" name="item[2][address]" />
On form submit I get values with javascript function:在表单提交上,我使用 javascript 函数获取值:
var obj = serializeObject(form)
function serializeObject(form) {
var o = {};
var a = form.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
I get an object with dynamic length (3 shown in an example item[0], item[1], item[2]).我得到一个具有动态长度的对象(在示例项目 [0]、项目[1]、项目[2] 中显示为 3)。 Object will always have this number of levels, and "item" is always the same property, only can contain other properties (except name, email, and address shown)对象将始终具有此数量的级别,并且“项目”始终是相同的属性,只能包含其他属性(显示的姓名、电子邮件和地址除外)
var obj = {
item[0][name]:'a',
item[0][email]:'a1',
item[0][address]:'a2',
item[1][name]:'b',
item[1][email]:'b1',
item[1][address]:'b2',
item[2][name]:'c',
item[2][email]:'c1',
item[2][address]:'c2',
}
I want to convert it into this format:我想把它转换成这种格式:
var arr = [
{name:'a', email:'a1', address:'a2'},
{name:'b', email:'b1', address:'b2'},
{name:'c', email:'c1', address:'c2'}
]
What is the best way to convert it (without hardcoding like this):转换它的最佳方法是什么(无需像这样进行硬编码):
var arr = [];
var o1 = {name:'a', email:'a1', address:'a2'};
var o2 = {name:'b', email:'b1', address:'b2'};
var o3 = {name:'c', email:'c1', address:'c2'};
arr.push(o1);
arr.push(o2);
arr.push(o3);
You could take names in the form of a dot separated property string and get the parts and build new objects of it.您可以采用点分隔的属性字符串形式的名称,并获取部件并构建它的新对象。
function serializeObject(form) { const matches = form.querySelectorAll("input"), result = []; matches.forEach(input => input.name.split('.').reduce((o, k, i, a) => o[k]??= i + 1 === a.length? input.value: {}, result) ); console.log(result); }; const input = document.getElementById('input'); input.addEventListener('submit', () => serializeObject(input));
<form id="input" onsubmit="return false"> <input type="text" name="0.name" /> <input type="text" name="0.email" /> <input type="text" name="0.address" /> <br> <input type="text" name="1.name" /> <input type="text" name="1.email" /> <input type="text" name="1.address" /> <br> <input type="text" name="2.name" /> <input type="text" name="2.email" /> <input type="text" name="2.address" /> <br> <input type="submit"> </form>
your syntax doesn't work:你的语法不起作用:
let obj = {
item[0][name]:'a'
}
array brackets in key object is not ok syntax there after is ok:关键对象中的数组括号不正确 后面的语法是正确的:
let obj = {
item0name:'a'
}
So regarding your actual syntax, no iteration possible因此,关于您的实际语法,不可能进行迭代
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.