waka-waka-waka
waka-waka-waka

Reputation: 1045

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:

[
    {
        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!

Upvotes: 1

Views: 2054

Answers (1)

Ben
Ben

Reputation: 489

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]});

Upvotes: 1

Related Questions