[英]I update an array (key,value) object in javascript
How can I update an array (key,value) object? 如何更新数组(键,值)对象?
arrTotals[
{DistroTotal: "0.00"},
{coupons: 12},
{invoiceAmount: "14.96"}
]
I want to update the 'DistroTotal' to a value. 我想将'DistroTotal'更新为值。
I have tried 我努力了
for (var key in arrTotals) {
if (arrTotals[key] == 'DistroTotal') {
arrTotals.splice(key, 2.00);
}
}
Thanks .. 谢谢 ..
Since it sounds like you are trying to use a key/value dictionary. 因为听起来你正在尝试使用键/值字典。 Consider switching to using an object instead of an array here.
考虑在此处切换到使用对象而不是数组。
arrTotals = {
DistroTotal: 0.00,
coupons: 12,
invoiceAmount: "14.96"
};
arrTotals["DistroTotal"] = 2.00;
You're missing a level of nesting: 您错过了嵌套级别:
for (var key in arrTotals[0]) {
If you only need to work with that specific one, then just do: 如果您只需要使用特定的那个,那么只需:
arrTotals[0].DistroTotal = '2.00';
If you don't know where the object with the DistroTotal
key is, or there are many of them, your loop is a bit different: 如果你不知道带有
DistroTotal
键的对象在DistroTotal
,或者有很多对象,你的循环就会有所不同:
for (var x = 0; x < arrTotals.length; x++) {
if (arrTotals[x].hasOwnProperty('DistroTotal') {
arrTotals[x].DistroTotal = '2.00';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.