0

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.

fsi
  • 1,319
  • 1
  • 23
  • 51
  • what do you get when you log the output of ```typeof(products.subproducts);``` ? – ffflabs Jul 01 '14 at 14:43
  • it gives me undefined. – fsi Jul 01 '14 at 14:45
  • 1
    *"It keeps me returning true whenever I use `if(product.subproducts)` or `product.subproduct.name`, which cannot read name property undefined."* How can you get `true` if the code throws an error? – Felix Kling Jul 01 '14 at 14:47
  • For the product.subproducts, returns true, the product.subproduct.name returns undefined. – fsi Jul 01 '14 at 14:48
  • if ```typeof(products.subproducts);``` is undefined, null or any type except an object, then you know it's not a nested object :) – ffflabs Jul 01 '14 at 14:48
  • soooo i learned it wrong. – fsi Jul 01 '14 at 14:49
  • 2
    You do realize that `product.subproducts` is not the same as `product.subproduct`? Accessing `product.subproduct.name` is very wrong. – Felix Kling Jul 01 '14 at 14:49
  • 1
    You have an object that contains an array of objects. So `products.subproducts` isn't right, it should be `products[0].subproducts` – Matt Burland Jul 01 '14 at 14:50
  • Lots of other duplicates: http://stackoverflow.com/q/6927242/218196, http://stackoverflow.com/q/2631001/218196, http://stackoverflow.com/q/14782232/218196, http://stackoverflow.com/q/18381259/218196 . [Please use the search](https://stackoverflow.com/search?q=[javascript]+nested+property+exists). – Felix Kling Jul 01 '14 at 14:53
  • Please, I want to delete this question, because the error was misstype. – fsi Jul 01 '14 at 16:45

3 Answers3

1

I think you should try this:

products[0].hasOwnProperty('subproducts')

Ani
  • 4,473
  • 4
  • 26
  • 31
0

It appears that when you say subproducts doesn't exist, it does actually exist as an object, but with no properties. An object with no properties is still considered truthy in JavaScript, so that's why it always passes your truthy condition, and then throws the undefined error if you try to access the name property.

Example:

var obj = {};
console.log(obj ? true : false); // true

In this case you can test for the name existing:

$.each(company.products, function (j, product) {
    if(product.hasOwnProperty('subproducts') {
       if(product.subproducts.name){
           // it has subproducts AND the subproduct.name exists
       }     
    } else {
      // do this
    }
}
MrCode
  • 63,975
  • 10
  • 90
  • 112
-1

Your best bet would be to go through the object and check for an object:

function hasSubObject(products) {
    for(var key in products){
        if(!products.hasOwnProperty(key))continue;
        if(typeof products[key] === "object") return true;
    }
    return false;
}

Which is gross and will probably slow down if you have large objects

Nick
  • 158
  • 1
  • 7