【问题标题】:Retrieving objects own properties based on key根据键检索对象自己的属性
【发布时间】: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,但我的结果数组中仍然有isEmptycontains 等函数。

我正在使用这个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'
  }
}

虽然我只得到了CKSAVTD,但我也得到了这个:

如何在 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


    【解决方案1】:

    你可以检查typeof每个属性,看看它是否是一个函数:

    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' && typeof ref[key] !== 'function') {
          results.push(product);
        }
      }
      return results;
    }
    

    【讨论】:

    • 我必须在coffeescript中做这个检查,因为这个js是从那里生成的。
    【解决方案2】:

    我认为track by $index 应该足够了,您将不再收到该异常。

    <option ng-repeat="prod in products | track by $index" value="{{prod.ProductTypeCode}}">{{prod.ProductType}}</option>
    

    【讨论】:

    • 我需要track by 保持不变。不幸的是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2017-04-22
    • 2012-04-11
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多