Skip to content

Instantly share code, notes, and snippets.

@blairanderson blairanderson/code.md
Last active Aug 29, 2015

Embed
What would you like to do?
javascript is fun

How would you optimize this for readability,fun...?

I need to return null if the value cannot be found for var lastName = contact.properties.lastname.value;

basically if a nested property doesn't exist... what are some cool ways to prevent "Cannot read prop X of undefined"

current solution:

lastName = getSourceColumnValue(contact, "properties.lastname.value");
firstName = getSourceColumnValue(contact, "properties.firstname.value");

var getSourceColumnValue = function (object, dotPath) {
  return _.reduce(dotPath.split('.'), getAtIndex, object);
};

var getAtIndex = function (object, index) {
  if (object) {
    if (_.isObject(object[index]) && _.isEmpty(object[index])) {
      return null;
    }
    return object[index];
  } else {
    return null;
  }
};

@blairanderson

This comment has been minimized.

Copy link
Owner Author

commented Aug 14, 2014

attempting to avoid try/catch everywhere

@mhuggins

This comment has been minimized.

Copy link

commented Aug 14, 2014

var getAtIndex = function (object, index) {
  var result = object && _.isObject(object[index]) && object[index];
  return result || null;
}
@mhuggins

This comment has been minimized.

Copy link

commented Aug 14, 2014

also, if undefined is equally allowed as a result (i.e.: doesn't have to be null), then you could just do:

var getAtIndex = function (object, index) {
  return object && _.isObject(object[index]) && object[index];
}
@justinabrahms

This comment has been minimized.

Copy link

commented Aug 14, 2014

var get = function (object, dotPath, dotPathArr) {
  if (!dotPathArr) {
    dotPathArr = dotPath.split('.');
  }

  var val = object[dotPathArr.shift()];
  if (!dotPathArr.length) {
    return val;
  }
  return get(val, null, dotPathArr);
};
@mrgenixus

This comment has been minimized.

Copy link

commented Aug 14, 2014

Having implemented this, blair's version has fewer akward side effects vs. recursive implementation

@mrgenixus

This comment has been minimized.

@mrgenixus

This comment has been minimized.

Copy link

commented Aug 14, 2014

^^ I don't think that was the final version

@justinabrahms

This comment has been minimized.

Copy link

commented Aug 14, 2014

> var z = {
...   a: {
.....     b: 1
.....   }
... };
undefined
>
undefined
> var get = function (object, dotPath, dotPathArr) {
...   if (!dotPathArr) {
.....     dotPathArr = dotPath.split('.');
.....   }
...
...   if (!object) {
...     return;
...   }
...
...   var val = object[dotPathArr.shift()];
...   if (!dotPathArr.length) {
...     return val;
...   }
...   return get(val, null, dotPathArr);
... };
undefined
>
undefined
> get(z, 'a');
{ b: 1 }
> get(z, 'a.b');
1
> get(z, 'b.c');
undefined
> get({}, 'a');
undefined
> get(z, 'a.b.c.d.e');
undefined
> get(null, 'asdf');
undefined
> get(z, 'asdfasdfasdf');
undefined
> get(z, '');
undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.