所以我正在编辑一些代码,我想将另一个变量添加到数组中。下面的代码完美运行:
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;
};
};
我得到错误:
错误:submit.ProductMeta.attributes[aType][aValue] 未定义
submit.addAttributeValue@http://dubdelivery.com/js/controllers-submit.js:369:13
ProductMeta 定义为:submit.ProductMeta = {};
关于我应该如何处理这个问题有什么建议吗?
谢谢!
最佳答案
你的数组是空的,所以你不能在第一级之前设置第二级字段
如果你有
var obj = {};
你必须设置
obj.firstItem
之前
obj.firstItem.secondItem
这是一个 example
关于javascript - 在angularJS中向数组添加另一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272157/