简体   繁体   English

具有多个具有相同名称的输入的nodejs表单

[英]nodejs form with multiple inputs having same name

Let me thank you ahead first :) 让我先谢谢你:)

I am using express with node. 我在节点上使用快递。 I have a form of type 我有一种类型的形式

<form>
    <div id=row-1>
        <input name=item></input>
        <input name=price></input>
    </div>
    <div id=row-2>
        <input name=item></input>
        <input name=price></input>
    </div>
</form>

The problem is I have multiple inputs with the same name, and ideally in req.body, I would want my data to be of the form: 问题是我有多个具有相同名称的输入,理想情况下,在req.body中,我希望我的数据采用以下形式:

[
    {
        item: item1,
        price: price1
    },
    {
        item: item2,
        price: price2
    }
]

But, when I post the form data, this is what I get: 但是,当我发布表单数据时,这是我得到的:

{
    item: [item1, item2],
    price: [price1, price2]
}

Is there something that I might be missing? 我可能会缺少一些东西吗?

Thanks again! 再次感谢!

Can you try something like this ? 你可以尝试这样的事情吗?

var data; // this is what you got.
var i;
var newObjectArray = [];
for(i= 0; i<object.item.length; i++)
    newObjectArray.push({item: data.item[i], price: data.price[i]});

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

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