[英]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. 我想用数据覆盖某些allOrders [i],类似于我创建一个新的方法。 For some reason I can't figure out what to search on. 由于某种原因,我不知道要搜索什么。
I have an array of objects allOrders. 我有一个对象数组allOrders。
I have an object BusinessCard. 我有一个对象BusinessCard。 I take the form fields, serialize() them, clean up the data with a regex, then push the them into an array. 我采用表单字段,对它们进行serialize(),使用正则表达式清理数据,然后将它们压入数组。
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. 我最好的猜测是allOrders [i] .push,但是在我看来,我必须编写一个新函数来替换对象中的每个属性。
Right now I am using(because using serialize() on the form inputs doesn't help me at all: 现在我正在使用(因为在表单输入上使用serialize()根本对我没有帮助:
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'); 含义我可能正在使用currentOrder = getFormData('businessCardForm'); then allOrders[i] = currentOrder; 然后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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.