2

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;

2
  • 2
    allOrders[i] = { "quantity": value1, "fullname": value2, ... } Commented Jun 7, 2013 at 18:57
  • When you wrote this I thought of my JSON.parse and that is what it puts it in as. Is that what you are saying (see my edit to OP) Commented Jun 7, 2013 at 19:14

1 Answer 1

5

Seems odd that you would be updating all items with the selector's you're using, but I would wrap up getting the updated order information then, you can run thru a loop.

Depending on your output, as long as it's outputing the respective properties and values of an order object you could just do:

for(int i =0; i < allOrders.length; i++){ var currentFormId = '' // update this for each iteration. allOrders[i] = getFormData(currentFormId); } allOrders[i] = getUpdatedOrder(); function getUpdatedOrder() { var order = {}; order.quantity = $('#bcQuantity').val(); order.fullname = $('#fullName').val(); order.title = $('#Title').val(); order.cell = $('#CellNumber').val(); order.office = $('#OfficeNumber').val(); order.fax = $('#FaxNumber').val(); order.email = $('#EmailAddress').val(); order.address = $('#Address').val(); order.website = $('#website').val(); order.price = $('#bcCostBeforeCart').text(); return order; } 
Sign up to request clarification or add additional context in comments.

1 Comment

I just edited my answer. I forgot to add my function to getFormData that I need to assign. Will you check my edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.