2017-04-17 85 views
0

每当我评估,看看我的对象,如果一个属性存在,我不断收到以下错误消息,当不确定是不是一个对象。错误:检查是否对象的属性存在

我该如何解决?

if(++responseCount === products.length) { 
     products.sort(function(a, b) { 
     if((a.result.hasOwnProperty('rawMisMatchPercentage')) && (b.result.hasOwnProperty('rawMisMatchPercentage'))) { 
      return a.result.rawMisMatchPercentage - b.result.rawMisMatchPercentage; 
     } 
     }); 
     return products.slice(0, 3); 
    } 
+0

所以a.result是不是一个对象。确保它是。 – dfsq

+0

'a.result'的价值是什么? –

+0

因此,在我检查'a.result.hasOwnProperty'之前,我需要额外检查'a.result'吗? – methuselah

回答

1

你的条件应该是

(a && a.result && a.result.hasOwnProperty('rawMisMatchPercentage') && (b && 
b.result && b.result.hasOwnProperty('rawMisMatchPercentage'))