将另一个变量添加到 angularJS 中的数组

Adding another variable to an array in angularJS

所以我正在编辑一些代码,我想向数组添加另一个变量。下面的代码完美运行:

submit.addAttributeValue = function() {
    var aValue = submit.status.newAttributeValue;
    var aType = submit.status.selectedAttributeType;
    console.log('adding value', aValue, aType)
    if(aValue && aType ) {
        submit.ProductMeta['attributes'][aType][aValue] = true;
    };
};

然后我将变量 aPrice 添加到函数中:

submit.addAttributeValue = function() {
    var aValue = submit.status.newAttributeValue;
    var aType = submit.status.selectedAttributeType;
    var aPrice = submit.status.newAttributePrice;
    console.log('adding value', aValue, aType, aPrice)
    if(aValue && aType ) {
        submit.ProductMeta['attributes'][aType][aValue][aPrice] = true;
    };
};

我收到错误:

Error: submit.ProductMeta.attributes[aType][aValue] is undefined submit.addAttributeValue@http://dubdelivery.com/js/controllers-submit.js:369:13

ProductMeta 定义为:submit.ProductMeta = {};

关于我应该如何处理这个问题有什么建议吗?

谢谢!

查看调试器或仅查看控制台日志 "aType"、"aValue" 和 "aPrice"。我打赌 "aValue" 是未定义的。

如果您打算以这种方式处理对象属性,则必须确保包含您的 属性 名称的变量确实具有值。

(顺便说一句,尤其是现在使用 ES6 类,您可能不应该以这种方式操作对象 property/values)。

你的数组是空的,所以你不能在第一级之前设置第二级字段

如果你有

var obj = {};

你必须设置

obj.firstItem

之前

obj.firstItem.secondItem

这是一个example