【问题标题】:ecma script for in loop and hasOwnProperty have a strange behaviour用于循环和 hasOwnProperty 的 ecma 脚本有一个奇怪的行为
【发布时间】:2020-01-04 14:04:42
【问题描述】:

我想使用 DTO 转换从 NoSQL 数据库获取的对象,因此,我在 for..in 循环中检查该对象以仅获取我想要保留的内容:

for (const attribute in result) {
    if (result.hasOwnProperty(attribute)) {
        console.log(`${attribute} belongs to object!`);
    }
}

不知道为什么:

  • 我必须使用hasOwnProperty 方法循环对象来获取属性
  • 我的对象有一个“营养”属性,但是...从来没有安慰过

这是原始对象的一部分:

...
nutriments:
   { sugars: 6.5,
     'nova-group_serving': 4,
     fiber_value: 2.5,
     'nutrition-score-uk_100g': 1,
     energy_value: 1160,
     salt_100g: 1.08,
     'nutrition-score-uk': 1,
     fiber_100g: 2.5,
     proteins: 8.5,
     'nova-group_100g': 4,
     carbohydrates_unit: 'g',
     'saturated-fat_100g': 0.4,
     'nutrition-score-fr_100g': 1,
     salt_unit: 'g',
     'saturated-fat_unit': 'g',
     sugars_100g: 6.5,
     sugars_value: 6.5,
     'saturated-fat_value': 0.4,
     carbohydrates_value: 49.2,
     fat_unit: 'g',
     fiber: 2.5,
     proteins_value: 8.5,
     fat_value: 4.3,
     sugars_serving: 5.13,
     sodium_value: 0.43200000000000005,
     fiber_serving: 1.98,
     sodium_unit: 'g',
     energy_serving: 916,
     sodium_serving: 0.34099999999999997,
     proteins_unit: 'g',
     carbohydrates: 49.2,
     energy: 1160,
     salt_value: 1.08,
     sodium_100g: 0.43200000000000005,
     'nova-group': 4,
     'saturated-fat_serving': 0.316,
     proteins_serving: 6.72,
     'nutrition-score-fr': 1,
     energy_100g: 1160,
     energy_unit: 'kJ',
     fiber_unit: 'g',
     'carbon-footprint-from-known-ingredients_product': 416,
     sugars_unit: 'g',
     proteins_100g: 8.5,
     'carbon-footprint-from-known-ingredients_100g': 75.6,
     carbohydrates_serving: 38.9,
     salt_serving: 0.8530000000000001,
     fat_serving: 3.4,
     salt: 1.08,
     carbohydrates_100g: 49.2,
     'saturated-fat': 0.4,
     fat_100g: 4.3,
     fat: 4.3,
     'carbon-footprint-from-known-ingredients_serving': 59.7,
     sodium: 0.43200000000000005 },
...

我编辑我的 for in 循环以跟踪“属性”并列出了属性“营养”,但是...result['nutriments'] 未定义且result.hasOwnProperty('nutriments') 返回 false...

for (const attribute in result) {
    console.log(`Discovering ${attribute} belongs to object!`);

    if (result.hasOwnProperty(attribute)) {
        console.log(`${attribute} belongs to object!`);
    }
}

这种行为对于其他一些对象属性是可观察到的,但我可以使用result.attributeName 获取属性值。

那么,什么可以解释这种行为呢?

【问题讨论】:

    标签: json typescript for-loop


    【解决方案1】:

    hasOwnProperty 为继承的属性返回 false。我的猜测是 nutriments 属性实际上是由这个对象继承的。我们使用它实际上是为了避免一些继承的属性。

    另一种可能性是,该对象要么是 Proxy,要么其某些属性受 .defineProperty 保护,这使得它们要么不可迭代,要么不可“获取”。

    需要更多信息才能进行正确的诊断/解决方案。

    【讨论】:

      【解决方案2】:

      对不起这个问题......我找到了探索我自己的猫鼬模式的解决方案,即使MongoDB返回文档的全部内容,这个由定义的模式处理,并且......我忘了添加“营养”属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-30
        • 1970-01-01
        • 1970-01-01
        • 2019-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多