这是我的代码:

console.log("findAllInvoice Result: " + JSON.stringify(data, null, 4));
console.log("hasOwnProperty Result: " + data.hasOwnProperty("totalInvoice"));
if (data.hasOwnProperty("totalInvoice")) {
    var myTotal = data.totalInvoice;
}
console.log("invoice Sum: " + myTotal)
console.log("=====RESOLVE findAllInvoice=====")
resolve(data);


这是console.log的结果:

=====RESOLVE findMonthBookings=====
findAllInvoice Result: [
    {
        "totalInvoice": 48758
    }
]
hasOwnProperty Result: false
invoice Sum: 0
=====RESOLVE findAllInvoice=====


我不明白吗?当属性明显存在时,如何返回false

最佳答案

尝试将第二行更改为此:

console.log("hasOwnProperty Result: " + data[0].hasOwnProperty("totalInvoice"));


它需要使用data[0]而不只是data

如您所述,findAllInvoice是一个数组,其中包含一个元素,该对象是对象[{"totalInvoice": 48758}]。您需要在该对象而不是包含数组上调用hasOwnProperty

关于javascript - JS hasownproperty无法正常工作,无论我做什么,它都会返回false,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44195854/

10-10 00:36