简体   繁体   中英

How do I overwrite object properties in an array?

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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