-1

After spending many hours today searching for a specific condition functionality and testing my modified finds. I thought I share my coding snippets and knowledge here. I'm not too good at answering other questions so I thought it would do to post my finds and hope it would benefit others. Since this site has been really helpful with my projects.

Anyway. At some point when you get to a stage scripting more complicated scripts. You'll most likely bump into a problem were you'll need to check deep in objects if a certain property exists. This has occurred mostly with JSON API from my experience.

Example Issue

if (someObject && someObject.nextObject && someObject.nextObject.andAnother) {
    // do something if all of this exists
}

Trying to check the deepest property, when one property in the middle doesn't exist, will throw a reference error.

ReferenceError: something is not defined

At some point these can get really long and messy to work with. Which is pretty much silly.

Trying to Tidy Like That Makes JSLint Complain & Can Be Harder for Another Scripter to Read

JSLint doesn't like when code ends up like this when scripters try to reduce the amount of characters on one line. Even I find it more difficult to read with other nested conditions together:

if (someObject
    && someObject.nextObject
    && someObject.nextObject.andAnother) {
    // do something if all of this exists
}
Nova
  • 2,000
  • 3
  • 14
  • 24

1 Answers1

0

There's multiple ways of making this less clutter. Below is an example of some different versions of snippet functions for this object.

var object = {
    nextObject: {
        anArray: [{
            value: true
        }]
    }
};

Boolean Function with String Perimeter

var hasDeepProperty = function (obj, pathString) {
    var i, properties = pathString.split("."), l = properties.length;
    for (i = 0; i < l; i++) {
        if (obj.hasOwnProperty(properties[i])) {
            obj = obj[properties[i]];
        } else {
          return false;
        }
    }
    return true;
};
console.log(hasDeepProperty(object, "nextObject.0.value")); // returns true
console.log(hasDeepProperty(object, "nextObject.doesNotExist.value")); // returns false

Boolean Function with Array Perimeter

var hasDeepProperty = function (obj, pathArray) {
    var i, properties = pathArray, l = properties.length;
    for (i = 0; i < l; i++) {
        if (obj.hasOwnProperty(properties[i])) {
            obj = obj[properties[i]];
        } else {
          return false;
        }
    }
    return true;
};
console.log(hasDeepProperty(object, ["nextObject", 0, "value"])); // returns true
console.log(hasDeepProperty(object, ["nextObject", "doesNotExist", "value"])); // returns false

Prototype Boolean Function with String Perimeter

Object.prototype.hasDeepProperty = function (pathString) {
    var i, properties = new String(pathString).split("."), l = properties.length, obj = new Object(this);
    for (i = 0; i < l; i++) {
        if (obj.hasOwnProperty(properties[i])) {
            obj = obj[properties[i]];
        } else {
          return false;
        }
    }
    return true;
};
console.log(object.hasDeepProperty("nextObject.0.value")); // returns true
console.log(object.hasDeepProperty("nextObject.doesNotExist.value")); // returns false

Prototype Boolean Function with Array Perimeter

Object.prototype.hasDeepProperty = function (pathArray) {
    var i, properties = pathArray, l = properties.length, obj = new Object(this);
    for (i = 0; i < l; i++) {
        if (obj.hasOwnProperty(properties[i])) {
            obj = obj[properties[i]];
        } else {
          return false;
        }
    }
    return true;
};
console.log(object.hasDeepProperty(["nextObject", 0, "value"])); // returns true
console.log(object.hasDeepProperty(["nextObject", "doesNotExist", "value"])); // returns false

I'm aware that declaring with the new keyword that isn't a function simulated as a class is discouraged. But for some reason Prototype causes slice method not to exist for string and same for hasOwnProperty for the object.

I hope others find this useful and there be something like this to be added on the next version of JavaScript.

This my first: post my own QnA. And Happy New Year everyone.

Nova
  • 2,000
  • 3
  • 14
  • 24