【发布时间】:2019-06-05 20:06:59
【问题描述】:
我正在尝试使用咖啡脚本从一个对象中获取keys,其中key 是一个特定值,但我还获取函数属性以及对象自身的属性。
在获取函数属性时出现此错误,因为这些函数属性上不存在 track by 表达式。
[ngRepeat:dupes]不允许在转发器中重复。使用“track by”表达式指定唯一键。中继器:
prod in products | track by prod.ProductTypeCode,重复键:undefined
这是track by 表达式:
<option ng-repeat="prod in products | track by prod.ProductTypeCode"
value="{{prod.ProductTypeCode}}">{{prod.ProductType}}
</option>
我在for of 循环中指定了own key,但我的结果数组中仍然有isEmpty 和contains 等函数。
我正在使用这个for of 循环。
product for own key, product of $scope.products when key isnt 'RTMT'
这是我的$scope.Products,其中包含密钥。
{
CK: {
ProductCategoryCode: 'DEP',
ProductCategory: 'Deposit',
ProductTypeCode: 'CK'
},
RTMT: {
ProductCategoryCode: 'RTMT',
ProductCategory: 'Retirement',
ProductTypeCode: 'IRA'
},
SAV: {
ProductCategoryCode: 'DEP',
ProductCategory: 'Deposit',
ProductTypeCode: 'SAV'
},
TD: {
ProductCategoryCode: 'DEP',
ProductCategory: 'Deposit',
ProductTypeCode: 'TD'
}
}
虽然我只得到了CK、SAV 和TD,但我也得到了这个:
如何在 coffeescript 中获取 Object 类型而不是 function 类型?
使用编译后的 javascript,它似乎在这个 sn-p 中工作,但在 Firefox 的调试窗口中也显示了这些功能。
// Generated
var hasProp = {}.hasOwnProperty,
indexOf = [].indexOf || function(item) {
for (var i = 0, l = this.length; i < l; i++) {
if (i in this && this[i] === item) return i;
}
return -1;
};
var obj = {
CK: {
ProductCategoryCode: 'DEP',
ProductCategory: 'Deposit',
ProductTypeCode: 'CK'
},
RTMT: {
ProductCategoryCode: 'RTMT',
ProductCategory: 'Retirement',
ProductTypeCode: 'IRA'
},
SAV: {
ProductCategoryCode: 'DEP',
ProductCategory: 'Deposit',
ProductTypeCode: 'SAV'
},
TD: {
ProductCategoryCode: 'DEP',
ProductCategory: 'Deposit',
ProductTypeCode: 'TD'
}
};
console.log(getValues(obj));
function getValues(obj) {
// Generated
var key, product, ref, results;
ref = obj; // $scope.products;
results = [];
for (key in ref) {
if (!hasProp.call(ref, key)) continue;
product = ref[key];
if (key !== 'RTMT') {
results.push(product);
}
}
return results;
}
【问题讨论】:
标签: javascript angularjs coffeescript angularjs-ng-repeat