简体   繁体   English

JS hasownproperty无法正常工作,无论我做什么,它都会返回false

[英]JS hasownproperty is not working, it returns false no matter what I do

Here is my code: 这是我的代码:

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);

Here is the result of the console.log : 这是console.log的结果:

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

I don't get it?! 我不明白吗? How can it return false when the property is clearly there? 当属性明显存在时,如何返回false

Try and change the second line to this: 尝试将第二行更改为此:

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

It needs to use data[0] instead of just data . 它需要使用data[0]而不是data

As you posted, findAllInvoice is an array with one element that is an object [{"totalInvoice": 48758}] . 如您所发布的, findAllInvoice是一个数组,其中一个元素是对象[{"totalInvoice": 48758}] You need to call hasOwnProperty on that object instead of on the containing array. 您需要在该对象而不是包含数组上调用hasOwnProperty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM