1

I was wondering whether in newer versions of javascript there is an alternative way of the dot notation which returns an object like nil, but still can use other .dot elements. I have code like this:

 var a = venue.photos.groups[0].items[0].prefix;

and I get an error like

Cannot read property 'items' of undefined

and looking for something similar to

if (venues.photos && venue.photos.groups && venue.photos.groups[0].items ) {
  a=venue.photos.groups[0].items[0].prefix;
} else {
  a=null;
}

For those who know objective C i would like something of the Form:

NSObject * val = [[a b] c] .. n]
Patrick Klitzke
  • 1,519
  • 2
  • 14
  • 24
  • you could use a try/catch block - even in the ye olde javascript – Jaromanda X Jan 15 '16 at 04:18
  • 1
    how does a, b, c, and n in the objective C example relate to the question? - as you go 6 levels deep in javascript – Jaromanda X Jan 15 '16 at 04:20
  • 1
    @JaromandaX that's just because objective-c never checks if the receiver of a message exists, and this notation can be used to access properties via their generated getters. some would argue it is a flaw of the language. what you are looking for is closer to Swifts optionals. – njzk2 Jan 15 '16 at 04:21
  • or the existential or elvis operator in various languages, for example coffeescript. – njzk2 Jan 15 '16 at 04:21
  • @njzk2 - nevermind - it would've been interesting to see how the parts in Obective C relate to the code in javascript, I understand (by inference as I've never touched Objective C, but I'm clever enough to infer) what the code does – Jaromanda X Jan 15 '16 at 04:24
  • 2
    Possible duplicate of [Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?](http://stackoverflow.com/questions/6613952/is-there-a-null-coalescing-elvis-operator-or-safe-navigation-operator-in-javas) – Aᴍɪʀ Jan 15 '16 at 04:30

5 Answers5

2

Use _.get from lodash

Their example code:

var object = { 'a': [{ 'b': { 'c': 3 } }] };

_.get(object, 'a[0].b.c'); // → 3

_.get(object, ['a', '0', 'b', 'c']); // → 3

_.get(object, 'a.b.c', 'default'); // → 'default'

So you can do

var a = _.get(venue, 'photos.groups[0].items[0].prefix');
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
1

I don't think so. There is no feature like that you want in ES6 neither ES7 http://es6-features.org/#Constants

EntrustName
  • 421
  • 6
  • 19
1

You can try this:

var defaultObject = "";    //this is the default value returned if the property is not found, you can return an empty object like {} too
Object.prototype.get = function(name) {
    if (this != undefined && this[name] != undefined)
        return this[name];
    else
        return defaultObject;
}
Object.prototype.defaultTo = function(value) {
    if (this === defaultObject)
        return value;
    else
        return this;
}

Usage:

var venue = { photos: { groups: [] } };
var a = venue.get("photos").get("groups").get(0).get("items").get(0).get("prefix").defaultTo("No prefix found");
mylee
  • 1,293
  • 1
  • 9
  • 14
0

I was wondering whether in newer versions of javascript there is an alternative way of the dot notation which returns an object like nil, but still can use other .dot elements.

No, there is nothing like this. There have been discussions about such an operator for ES7, but there was no consensus.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

With Modern Javascript you can do it :

var a = venue?.photos?.groups?.[0].items?.[0].prefix;