我试图在一个对象中添加一个键,如果它不存在,或者如果它已经存在就增加它的计数。下面的代码可以正确地添加新的键,但如果该键已经存在,则不会增加其计数。相反,它返回{UniqueResult1:NaN, UniqueResult2:NaN}
。
let detectionHash = {};
function onDetected(result) {
detectionHash[result.code]++;
if(detectionHash[result.code] >= 5) {
//here's where I want to be
}
}
如何在键已存在的情况下增加键值的计数?