I would like to overwrite a certain allOrders[i] with data, similar to how I create a new one. For some reason I can't figure out what to search on.
I have an array of objects allOrders.
I have an object BusinessCard. I take the form fields, serialize() them, clean up the data with a regex, then push the them into an array.
allOrders.push(new BusinessCard(currentOrder.quantity, currentOrder.FullName, currentOrder.Title, currentOrder.CellNumber, currentOrder.OfficeNumber, currentOrder.FaxNumber, currentOrder.EmailAddress, currentOrder.Address, currentOrder.website, currentOrder.price)); I've tried searching for overwriting existing object properties in an array and the likes and haven't figured out what to do here.
My best guess was allOrders[i].push -- but it seems to me that I have to write a new function to replace each property in the object.
Right now I am using(because using serialize() on the form inputs doesn't help me at all:
allOrders[i].quantity = $('#bcQuantity').val(); allOrders[i].fullname = $('#fullName').val(); allOrders[i].title = $('#Title').val(); allOrders[i].cell = $('#CellNumber').val(); allOrders[i].office = $('#OfficeNumber').val(); allOrders[i].fax = $('#FaxNumber').val(); allOrders[i].email = $('#EmailAddress').val(); allOrders[i].address = $('#Address').val(); allOrders[i].website = $('#website').val(); allOrders[i].price = $('#bcCostBeforeCart').text(); There has to be a smarter way to do this. Thank you.
EDIT:
function getFormData(formId) { var currentForm = '#' + formId; var currentPrice = $('#bcCostBeforeCart').text(); var currentFormData = $(currentForm).serialize(); var currentFormDataFinal = currentFormData + '&price=' + currentPrice; return JSON.parse('{"' + decodeURI(currentFormDataFinal.replace(/\+/g, " ").replace(/&/g, "\",\"").replace(/=/g, "\":\"")) + '"}'); } MEANING i could be using currentOrder = getFormData('businessCardForm'); then allOrders[i] = currentOrder;
allOrders[i] = { "quantity": value1, "fullname": value2, ... }