I have a javascript formset that submits multiple data, if I am getting all the post data, I have something like the below
firstname1: "John",
lastname1: "Doe",
firstname2: "Mary",
lastname2: "Allinson",
firstname3: "David"
lastname3: "Mark",
eventDesctiption: "Lorem Ipsum...",
eventDate: "Lorem Ipsum..."
I have an hidden field that holds the number of names submitted, in this case; its 3. I want to be able to loop through the names and put them in an array of objects before posting to an API, I want to be able to achieve the below
{
eventDesctiption: "Lorem Ipsum...",
eventDate: "Lorem Ipsum...",
people: [
{firstname: "John", lastname: "Doe"},
{firstname: "Mary", lastname: "Allinson"},
{firstname: "David", lastname: "Mark"},
]
}
I tried the below, but it seem to concatenate the index with the value, which is not what I want
peopleArray = new Array();
for(var i=1; i<=no_of_ben; i++){
var peopleObject = {};
peopleObject.firstname = data.firstname + 'i';
peopleObject.lastname = data.lastname + 'i';
peopleArray.push(peopleObject);
}
How to do this without concatenating the index