-1

I'm working on a dynamic structure, as a basic example:

var myObj = {
  "fruits" : {
    "apples" : {
      "color" : "red",
      "price" : "$0.49"
    }
  },
  "colors" : {}
};

So I'm anticipating that for example the fruit, "orange" does not exist. I want to test that it doesn't exist, so I can add it.

I see in console that you are told "undefined" if you try to access an "object" that doesn't exist.

That's what I'm looking for but I don't want the error in the console, I want to know/anticipate that it's empty so I can add to it.

If I do something like

console.log(myObj.fruits.apples.color);

I'd get red, so if I did that to orange

console.log(myObj.fruits.orange.color);

it would say undefined, at least if I tried

console.log(myObj.fruits.apples.weight);

That should be undefinied, my jsfiddle is not doing so hot, I can't reproduce that undefined console.log message at the moment

Okay, so it seems that provided the object exists, you can test for an undefined property like the weight example above.

I'm not looking for that, I'm looking to test if an object like "coconut" doesn't exist, so I can add it to the object array using extend. But I don't want the red console log error, I want to be able to anticipate it using a comparison like:

if (myObj.fruits.coconut === "undefined") {
  // means it doesn't exist, add it
}

I realize with my own test, that would produce the error, not the undefined like the apple weight console log demo above

4 Answers4

1

You should check for the first property that might not exist: so don't check the coconut.color property before you are sure that coconut exists.

If the property you check for should be an object (which is your case) then you can do so with the ! operator:

if (!myObj.fruits.coconut) myObj.fruits.coconut = { color: 'brown' };
trincot
  • 317,000
  • 35
  • 244
  • 286
0

If you want to test for the presence of a property on an object you can use the Object.prototype.hasOwnProperty():

myObj.fruits.hasOwnProperty("apples") //true
myObj.fruits.hasOwnProperty("oranges") //false
hackerrdave
  • 6,486
  • 1
  • 25
  • 29
0

As someone pointed out in the comments, you have at least two ways.

First one, the simplest, is simply doing

if(!myObj.fruits.cononut)
    // do stuff

However this is not very safe since it "does stuffs" even if .coconut is false, for example.

What people usually does is

if(typeof myObj.fruits.cononut === "undefined")
      // do stuff

that checks if the field actually doesn't exist and is not set (=== also checks for the same type).

However, this time I think you can ever try with this (not tested deeply):

if(!("cononut" in myObj.fruits))
    // do stuff

That checks for the existance of the key.

EDIT

As @hackerrdave pointed out, you can also use .hasOwnProperty method.

Samuele Pilleri
  • 734
  • 1
  • 7
  • 17
0

Underscore js contains _.has link

_.has(object, key) Does the object contain the given key? Identical to object.hasOwnProperty(key), but uses a safe reference to the hasOwnProperty function, in case it's been overridden accidentally.

_.has({a: 1, b: 2, c: 3}, "b");
=> true

Obviously, hackerrdave answer is suffice, but if you're using underscorejs then it's slightly simpler.

EmJay
  • 3
  • 1
  • With plain JavaScript you can already write `key in obj`. It seems a bit overkill to use a library for this and still write something that is at least as long as that ( `_has.(obj, key)` ). – trincot Dec 27 '16 at 14:37