AJF*_*DIA 3 jquery json amazon-web-services
我正在使用亚马逊产品 api,我的请求返回 xml,其中编码为 json。
我的 Feed 中的某些商品没有价格,因此出现以下错误
TypeError: this.ItemAttributes.ListPrice is undefined
Run Code Online (Sandbox Code Playgroud)
但是我可以检索销售价格。所以我想基本上看看
this.ItemAttributes.ListPrice 是未定义的,如果是这样,那么寻找这个...
this.Offers.OfferListing.Price.FormattedPrice
我怎样才能在 jQuery 中做到这一点?
我试过这个..
if (this.ItemAttributes.ListPrice != null){
var price = this.ItemAttributes.ListPrice;
}else{
var price = this.Offers.OfferListing.Price.FormattedPrice
}
Run Code Online (Sandbox Code Playgroud)
几乎。您想检查undefined,而不是null。
一种方法可能是:
var price;
if (this.ItemAttributes.ListPrice !== undefined) {
price = this.ItemAttributes.ListPrice;
}
else {
price = this.Offers.OfferListing.Price.FormattedPrice;
}
Run Code Online (Sandbox Code Playgroud)
如果您想覆盖所有虚假值(空、未定义、零...),您可以将第一行变成:
if (this.ItemAttributes.ListPrice) {
...
Run Code Online (Sandbox Code Playgroud)
你可以用 || 写得更简洁 或者 ?操作符,但一定要保持它的可读性。
| 归档时间: |
|
| 查看次数: |
18700 次 |
| 最近记录: |