How can I check if subproducts object exists in my JSON?
"products":[ { "id":5002, "description":"i dont know", "position":1, "weight":1, "subproducts":{ "name":"test", "description":"description of test" } } ], It keeps me returning true whenever I use if(product.subproducts) and product.subproduct.name, which cannot read name property undefined.
$.each(company.products, function (j, product) { if(product.hasOwnProperty('subproducts') { //do something } else { // do this } } UPDATED: forgot to say that, for each products, contains subproducts or not.
typeof(products.subproducts);?if(product.subproducts)orproduct.subproduct.name, which cannot read name property undefined." How can you gettrueif the code throws an error?product.subproductsis not the same asproduct.subproduct? Accessingproduct.subproduct.nameis very wrong.products.subproductsisn't right, it should beproducts[0].subproducts