简体   繁体   English

如何覆盖数组中的对象属性?

[英]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.

相关问题 如何从数组中向对象添加属性? - How do I add properties to an object from an array? 如何在JavaScript中将对象属性添加到数组中? - How do I add object properties into an array in JavaScript? 如何访问数组中对象的属性? - How do I access an object's properties in an array? 如何在不更改原始数组的情况下破坏对象属性? - How do i destruct object properties without mutating original array? 如何从数组中的 JavaScript 对象中删除属性? - How do I remove properties from JavaScript object in array? 如何将数组项添加到对象属性 - How do I add array items to object properties 我如何循环遍历数组中每个 object 的属性,每个 object 的属性都不同。 这是 Javascript - How do i loop through the properties of each object in the array, the properties are different in each object. This is Javascript 如何从基础对象继承函数并在javascript中覆盖它 - How do I inherit a function from a base object and overwrite it in javascript 如何访问位于数组内部,位于数组内部的对象的属性? - How do I access properties of an object that's inside of an array that's inside of an array that's inside of an array? 如何覆盖状态中的数组的初始条目? - How do I overwrite the initial entry from an array in state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM