How to check for the presence of certain strings in JSON, for example:

{ "id": "4", "name": "Weissgold-Verlobungsring mit Diamanten", "image": "/images/content/white-gold1.jpg", "oldprice": "14 500 €", "newprice": "16 100 €", "discount": "-10%" }, { "id": "5", "name": "Gold-Verlobungsring mit Diamanten", "image": "/images/content/white-gold2.jpg", "oldprice": "110 500 €", "newprice": "16 100 €", "hit": "hit" }, { "id": "6", "name": "Ring zur Verlobung mit Diamanten", "image": "/images/content/white-gold3.jpg", "oldprice": "110 500 €", "newprice": "16 100 €", "new": "new" }, { "id": "7", "name": "Ring zur Verlobung mit Diamanten", "image": "/images/content/white-gold1.jpg", "oldprice": "110 500 €", "newprice": "16 100 €" }, { "id": "8", "name": "Ring zur Verlobung mit Diamanten", "image": "/images/content/white-gold3.jpg", "oldprice": "110 500 €", "hit": "hit" } 

There is a discount, how to check for its availability?

 $(document).ready(function(){ $('.b-pagination__link--more').click(function(){ // вешаем на клик $.ajax({ url: 'json/product.json', //url: '/ajax/review.php', method: 'GET', dataType: 'json', success: function (json) { if (json['product'].length > 0) { var product = json['product']; $.each(product, function (id, data) { // console.log() var html = '<a href="#" data-product-id="' + data.id + '" title="" class="b-list-product__item col-md-3 col-sm-4 col-xs-6">\ <div class="b-list-product__link">\ <div class="b-list-product__background"></div>\ <div class="b-list-product__quick-preview">\ <span class="b-button-color b-list-product__link-preview">Übersicht</span>\ </div>\ </div>\ <img src="' + data.image + '" alt="" title="" class="b-list-product__image">\ <p class="b-list-product__info">' + data.name + '</p>\ <p class="b-list-product__price">' + data.newprice + '</p>\ <div class="b-list-product__old-price">\ <p class="b-list-product__old">' + data.oldprice + '</p>'; if (parseInt(data.discount) > 0) { html += '<div class="b-sale b-sale--list-product">' + data.discount + '</div>'; } html += '</div>\ </a>'; $('.b-list-product__item:last-child').before(html).parent('.b-list-product--catalog'); }); } } }); }) }); 

I tried this option

 if (parseInt(data.discount) > 0) { html += '<div class="b-sale b-sale--list-product">' + data.discount +</div>'; } 

But does not work!

    1 answer 1

    Try using the hasOwnProperty method,

     if (data.hasOwnProperty('discount')) { html += '<div class="b-sale b-sale--list-product">' + data.discount + '</div>'; }; if (data.hasOwnProperty('hit')) { html += '<div class="b-sale b-sale--list-product b-sale__hit">' + data.hit + '</div>'; }; if (data.hasOwnProperty('new')) { html += '<div class="b-sale b-sale--list-product b-sale__new">' + data.new + '</div>'; }; 
    • Well, only first JSON needs to be parsed - Vasily Barbashev
    • @ Vasily Barbashev, I agree, but the question was about js and json and related to how to check the presence of the key in the object, and not how to parse the received json data on the objects and find out what objects have such keys - LamerXaKer
    • updated the question) of course parseInt is wrong, it seems to return a number - drtvader
    • And if you try to use the proposed code? if(data.hasOwnProperty('discount')){....} - LamerXaKer
    • does not work. if (product.hasOwnProperty ('discount')> 0) {html + = '<div class = "b-sale b-sale - list-product">' + data.discount + '</ div>'; } does not output HTML - drtvader