繁体   English   中英

我在javascript中更新了一个数组(key,value)对象

[英]I update an array (key,value) object in javascript

如何更新数组(键,值)对象?

arrTotals[
{DistroTotal: "0.00"},
{coupons: 12},
{invoiceAmount: "14.96"}
]

我想将'DistroTotal'更新为值。

我努力了

    for (var key in arrTotals) {
        if (arrTotals[key] == 'DistroTotal') {
            arrTotals.splice(key, 2.00);
        }
    }

谢谢 ..

因为听起来你正在尝试使用键/值字典。 考虑在此处切换到使用对象而不是数组。

arrTotals = { 
    DistroTotal: 0.00,
    coupons: 12,
    invoiceAmount: "14.96"
};

arrTotals["DistroTotal"] = 2.00;

您错过了嵌套级别:

for (var key in arrTotals[0]) {

如果您只需要使用特定的那个,那么只需:

arrTotals[0].DistroTotal = '2.00';

如果你不知道带有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.

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