867

If I have a reference to an object:

var test = {};

that will potentially (but not immediately) have nested objects, something like:

{level1: {level2: {level3: "level3"}}};

What is the best way to check for the existence of property in deeply nested objects?

alert(test.level1); yields undefined, but alert(test.level1.level2.level3); fails.

I’m currently doing something like this:

if(test.level1 && test.level1.level2 && test.level1.level2.level3) {
    alert(test.level1.level2.level3);
}

but I was wondering if there’s a better way.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
user113716
  • 318,772
  • 63
  • 451
  • 440
  • 1
    you might want to check a tangentially related question that was asked recently http://stackoverflow.com/questions/2525943/adding-to-json-property-that-may-or-may-not-exist-yet – Anurag Apr 13 '10 at 16:21
  • See also http://stackoverflow.com/questions/10918488/testing-nested-objects-as-undefined-in-javascript – James McMahon Dec 21 '12 at 15:38
  • A couple of propositions there : http://stackoverflow.com/a/18381564/1636522 –  Sep 07 '13 at 07:27
  • Your current approach has a potential issue if level3 property is a false, in that case, even if the property exist will retur nfalse have a look at this example please https://jsfiddle.net/maz9bLjx/ – GibboK Jul 11 '15 at 06:18
  • 12
    simply you can use try catch also – Raghavendra Aug 12 '15 at 12:07
  • I am about to just open a question and ask if using a `try/catch` for this is a good idea, because i have been doing that lately and missing coffeescripts `?` which does this for you lol. – Leathan Aug 09 '17 at 13:34
  • Refer this link for answer https://stackoverflow.com/questions/2281633/javascript-isset-equivalent/56887380#answer-56887380 – bikash.bilz Jul 04 '19 at 12:03
  • Optional Chaining is what you want https://stackoverflow.com/a/60845999/2100372 – zoran404 Mar 25 '20 at 09:36
  • It needs a recursive solution that can be generally used to find deeply nested keys. This [post](https://www.techighness.com/post/javascript-find-key-path-in-deeply-nested-object-or-array/) might help. – Talha Awan Aug 31 '21 at 06:54

64 Answers64

719

You have to do it step by step if you don't want a TypeError because if one of the members is null or undefined, and you try to access a member, an exception will be thrown.

You can either simply catch the exception, or make a function to test the existence of multiple levels, something like this:

function checkNested(obj /*, level1, level2, ... levelN*/) {
  var args = Array.prototype.slice.call(arguments, 1);

  for (var i = 0; i < args.length; i++) {
    if (!obj || !obj.hasOwnProperty(args[i])) {
      return false;
    }
    obj = obj[args[i]];
  }
  return true;
}

var test = {level1:{level2:{level3:'level3'}} };

checkNested(test, 'level1', 'level2', 'level3'); // true
checkNested(test, 'level1', 'level2', 'foo'); // false

ES6 UPDATE:

Here is a shorter version of the original function, using ES6 features and recursion (it's also in proper tail call form):

function checkNested(obj, level,  ...rest) {
  if (obj === undefined) return false
  if (rest.length == 0 && obj.hasOwnProperty(level)) return true
  return checkNested(obj[level], ...rest)
}

However, if you want to get the value of a nested property and not only check its existence, here is a simple one-line function:

function getNested(obj, ...args) {
  return args.reduce((obj, level) => obj && obj[level], obj)
}

const test = { level1:{ level2:{ level3:'level3'} } };
console.log(getNested(test, 'level1', 'level2', 'level3')); // 'level3'
console.log(getNested(test, 'level1', 'level2', 'level3', 'length')); // 6
console.log(getNested(test, 'level1', 'level2', 'foo')); // undefined
console.log(getNested(test, 'a', 'b')); // undefined

The above function allows you to get the value of nested properties, otherwise will return undefined.

UPDATE 2019-10-17:

The optional chaining proposal reached Stage 3 on the ECMAScript committee process, this will allow you to safely access deeply nested properties, by using the token ?., the new optional chaining operator:

const value = obj?.level1?.level2?.level3 

If any of the levels accessed is null or undefined the expression will resolve to undefined by itself.

The proposal also allows you to handle method calls safely:

obj?.level1?.method();

The above expression will produce undefined if obj, obj.level1, or obj.level1.method are null or undefined, otherwise it will call the function.

You can start playing with this feature with Babel using the optional chaining plugin.

Since Babel 7.8.0, ES2020 is supported by default

Check this example on the Babel REPL.

UPDATE: December 2019

The optional chaining proposal finally reached Stage 4 in the December 2019 meeting of the TC39 committee. This means this feature will be part of the ECMAScript 2020 Standard.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • amazing logic Oo, what is Array.prototype.slice.call(arguments) doing? – Rodrigo Dias Nov 09 '12 at 18:17
  • 4
    `arguments` is not actually an array. `Array.prototype.slice.call(arguments)` converts it to a formal array. **[Learn](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments)** – deefour Nov 10 '12 at 01:00
  • 24
    this'd be [a lot more efficient](http://jsperf.com/checknested-efficiency) to do `var obj = arguments[0];` and start from `var i = 1` instead of copying the `arguments` object – Claudiu Oct 31 '13 at 19:45
  • Thanks for the function! I modified it to accept arguments in the format of ... (obj, 'level1.level2.level3') ... to keep usage easier and more readable – derrylwc Sep 03 '14 at 21:05
  • 2
    I put together a version with try/catch for austerity sake, and no surprise - performance is awful (except in Safari for some reason). There are some answers below that are pretty performant, along with Claudiu's modification which is also significantly more performant than selected answer. See jsperf here http://jsperf.com/check-if-deep-property-exists-with-willnotthrow – netpoetica Nov 27 '14 at 05:35
  • 1
    This is a pretty old answer, but I like it. Found one small issue though. if you pass in window as `obj`, you'll get an error in IE8 or lower. See http://stackoverflow.com/a/1186510/210827... might be better to use `!Object.prototype.hasOwnProperty.call(obj, args[i])` instead of `!obj.hasOwnProperty(args[i])`. – getWeberForStackExchange Apr 28 '15 at 00:19
  • use `lang.exists()` if you're using dojo. http://stackoverflow.com/questions/33741024/how-to-check-for-multiple-undefined-in-dojo-or-javascript – Vijin Paulraj Nov 18 '15 at 17:52
  • 3
    In ES6 the `args` variable declaration can be removed and and `...args` can be used as the second argument for the `checkNested` method. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Spread_operator – Vernon Dec 28 '16 at 15:49
  • 10
    This is a very unmaintainable. If any property keys change (they will), all devs on the project would have to 'string search' the entire codebase. This isn't really a solution to the problem, as it introduces a much bigger problem – Drenai Jan 14 '18 at 11:20
  • not work correctly with checkNested(test, 'level1', 'level2', 'level3', '5'). If the last param is number from 0 to 5 the method will return true that is incorrect. – Staxaaaa Jun 21 '19 at 00:51
  • that can be reproduced in case when the last property is array or array like – Staxaaaa Jun 21 '19 at 00:59
  • @cms for fixing that we should add extra or to condition `if (!obj || !obj.hasOwnProperty(args[i]) || ! ( Object.prototype.toString.call(obj) == '[object Object]' ))`. – Staxaaaa Jun 21 '19 at 01:24
  • @Staxaaaa, I see your point, but that would limit the property access. For example, let's say you want to access the `length` property on a string or array? Or the expression: `level1.level2.level3.length` I think should be valid. – Christian C. Salvadó Jun 21 '19 at 18:35
  • @CMS do you know when `const value = obj?.level1?.level2?.level3 ` this will be available in browsers? – Muhammad Usman Oct 22 '19 at 14:54
  • @MuhammadUsman, the feature currently is on Stage 3, but it will be presented in the [December 2019 reunion](https://github.com/tc39/agendas/blob/master/2019/12.md) of the TC39 committee by Daniel Rosenwasser to analyze its advancement to Stage 4. If it reaches Stage 4, you will start seeing it implemented by browser vendors next year. In the meantime, you can use the [babel plugin](https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining) I mention. – Christian C. Salvadó Oct 22 '19 at 15:06
  • looks great. why this example- const value = obj?.level1?.level2?.level3 not working in nodejs – Deepak Nov 28 '19 at 05:04
  • @Deepak it isn't working yet because the syntax is not yet officially standard. As the time of writing, it has been only implemented by Chrome 79 (see [compatibility table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#Browser_compatibility)). If you want to use this syntax, use Babel with the plugin I mention. – Christian C. Salvadó Nov 28 '19 at 12:28
  • so when is the release date for ECMA 2020? @CMS –  Feb 17 '20 at 01:12
  • @Artportraitdesign1, I think it will be official after the 02-04 June 2020 meeting at PayPal in Chicago. Since ES6 (2015), June is the month the ECMAScript yearly editions are released. – Christian C. Salvadó Feb 17 '20 at 03:26
  • 1
    @CMS **Babel 7.8.0** has ECMAScript 2020 support by default so we don't need to use `@babel/plugin-proposal-optional-chaining` anymore. – Snaker Feb 18 '20 at 12:35
  • obj.hasOwnProperty(level) will fail if obj is null. e.g. `checkNested({a:null}, 'a', 'b')`. Needs a check for null, I think.. – naught101 Mar 17 '20 at 07:52
379

Here is a pattern I picked up from Oliver Steele:

var level3 = (((test || {}).level1 || {}).level2 || {}).level3;
alert( level3 );

In fact that whole article is a discussion of how you can do this in javascript. He settles on using the above syntax (which isn't that hard to read once you get used to it) as an idiom.

Ben Creasy
  • 3,825
  • 4
  • 40
  • 50
Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99
  • 1
    So, if you fail at the first level, you keep searching through the entire pipe to finally get `undefined`, which was predictable. Not so interesting ;) –  Sep 05 '13 at 11:45
  • 8
    @wared I think it is interesting mostly for how concise it is. There is a detailed discussion of the performance characteristics in the linked post. Yes it always does all the tests, but it avoids creating temp vars, and you can alias {} to a var if you want to prevent the overhead of creating a new empty object each time. In 99% of cases I would not expect speed to matter, and in cases where it does there is no substitute for profiling. – Gabe Moothart Sep 05 '13 at 18:13
  • I understand now, I was apparently off topic :) That said, I like the idea of a reusable empty object. –  Sep 05 '13 at 19:59
  • 9
    @MuhammadUmer No, the point of `(test || {})` is that if test is undefined, then you're doing `({}.level1 || {})`. Of course, `{}.level1` is undefined, so that means you're doing `{}.level2`, and so on. – Joshua Taylor Apr 28 '15 at 16:06
  • 3
    @JoshuaTaylor: I think he means if `test` is not declared, there'll be a *ReferenceError*, but that's not a problem, because if it's not declared, there's a bug to be fixed, so the error is a good thing. –  Jan 02 '16 at 16:15
  • 43
    you said *"which isn't **that hard** to read **once** you **get used** to it"*. Well, **these** are signs you **know** already this is a **mess**. Then why suggest this solution? It is prone to typos and gives absolutely **nothing** to readability. Just look it! If i **have to** write an ugly line, it should asswell be **readable**; so i'm going to just stick with `if(test.level1 && test.level1.level2 && test.level1.level2.level3)` – Sharky Sep 30 '16 at 09:16
  • 11
    Unless I'm missing something, this won't work for boolean end-properties that might be false... sadly. Otherwise I love this idiom. – T3db0t Nov 17 '16 at 20:01
  • This solution works well if you need to drill down into nested properties that are arrays that themselves contain object containing the key data. – timebandit Mar 06 '17 at 18:16
  • Thoughts on how to employ this programmatically?... so that these laborious checks do not promulgate throughout the code. – Kwhitejr Jul 31 '18 at 23:23
  • 1
    `Readability > Performance` - Don't use this. `Performance > Readability` - absolutely use this. – David Barker May 17 '19 at 07:19
  • @JoshuaTaylor I just tested in browser it is saying test is not defined, or maybe I am doing something wrong? – Muhammad Usman Oct 22 '19 at 15:00
  • @MuhammadUmer you are correct it it saying "Uncaught ReferenceError: test is not defined" – Muhammad Usman Oct 22 '19 at 15:00
  • I just made an function based this approach ```const deepProp = (o, props) => { return props.reduce((a,b) => { let k = (a||{})[b]; a=k; return k}, o)}``` – Juliano Silva Nov 22 '22 at 23:04
290

Update

Looks like lodash has added _.get for all your nested property getting needs.

_.get(countries, 'greece.sparta.playwright')

https://lodash.com/docs#get


Previous answer

lodash users may enjoy lodash.contrib which has a couple methods that mitigate this problem.

getPath

Signature: _.getPath(obj:Object, ks:String|Array)

Gets the value at any depth in a nested object based on the path described by the keys given. Keys may be given as an array or as a dot-separated string. Returns undefined if the path cannot be reached.

var countries = {
        greece: {
            athens: {
                playwright:  "Sophocles"
            }
        }
    }
};

_.getPath(countries, "greece.athens.playwright");
// => "Sophocles"

_.getPath(countries, "greece.sparta.playwright");
// => undefined

_.getPath(countries, ["greece", "athens", "playwright"]);
// => "Sophocles"

_.getPath(countries, ["greece", "sparta", "playwright"]);
// => undefined
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Austin Pray
  • 4,926
  • 2
  • 22
  • 30
  • Lodash really needs a _.isPathDefined(obj, pathString) method. – Matthew Payne Jan 09 '15 at 16:34
  • @MatthewPayne It'd be nice perhaps, but it really isn't necessary. You could do it yourself really easily `function isPathDefined(object, path) { return typeof _.getPath(object, path) !== 'undefined'; }` – Vala May 12 '15 at 11:33
  • 13
    Lodash has this same functionality itself: `_.get(countries, 'greece.sparta.playwright', 'default'); // → 'default' _.has(countries, 'greece.spart.playwright') // → false` – Tom May 15 '15 at 18:23
  • even better would be _.result – Shishir Arora Aug 29 '15 at 08:50
  • If you need to determine multiple different paths consider: `var url = _.get(e, 'currentTarget.myurl', null) || _.get(e, 'currentTarget.attributes.myurl.nodeValue', null) || null` – Simon Hutchison May 04 '17 at 04:37
  • is there a similiar _.set function for ex _.set(obj, "prop1.prop2.prop3", "value", defaultInitialvalue);
    defaultInitialvalue can be used to set the prop if not present
    and in addition I wonder if _.get can be augmented to add function evaluation i.e _.get(e, 'currentTarget.attributes.myurl().node.value()')
    – sktguha May 04 '17 at 10:43
  • I like it that this exists, but one downside of this kind of "stringy" approach makes you lose the linting benefits.. – tkit Jun 29 '17 at 09:35
233

I have done performance tests (thank you cdMinix for adding lodash) on some of the suggestions proposed to this question with the results listed below.

Disclaimer #1 Turning strings into references is unnecessary meta-programming and probably best avoided. Don't lose track of your references to begin with. Read more from this answer to a similar question.

Disclaimer #2 We are talking about millions of operations per millisecond here. It is very unlikely any of these would make much difference in most use cases. Choose whichever makes the most sense knowing the limitations of each. For me I would go with something like reduce out of convenience.

Object Wrap (by Oliver Steele) – 34 % – fastest

var r1 = (((test || {}).level1 || {}).level2 || {}).level3;
var r2 = (((test || {}).level1 || {}).level2 || {}).foo;

Original solution (suggested in question) – 45%

var r1 = test.level1 && test.level1.level2 && test.level1.level2.level3;
var r2 = test.level1 && test.level1.level2 && test.level1.level2.foo;

checkNested – 50%

function checkNested(obj) {
  for (var i = 1; i < arguments.length; i++) {
    if (!obj.hasOwnProperty(arguments[i])) {
      return false;
    }
    obj = obj[arguments[i]];
  }
  return true;
}

get_if_exist – 52%

function get_if_exist(str) {
    try { return eval(str) }
    catch(e) { return undefined }
}

validChain – 54%

function validChain( object, ...keys ) {
    return keys.reduce( ( a, b ) => ( a || { } )[ b ], object ) !== undefined;
}

objHasKeys – 63%

function objHasKeys(obj, keys) {
  var next = keys.shift();
  return obj[next] && (! keys.length || objHasKeys(obj[next], keys));
}

nestedPropertyExists – 69%

function nestedPropertyExists(obj, props) {
    var prop = props.shift();
    return prop === undefined ? true : obj.hasOwnProperty(prop) ? nestedPropertyExists(obj[prop], props) : false;
}

_.get – 72%

deeptest – 86%

function deeptest(target, s){
    s= s.split('.')
    var obj= target[s.shift()];
    while(obj && s.length) obj= obj[s.shift()];
    return obj;
}

sad clowns – 100% – slowest

var o = function(obj) { return obj || {} };

var r1 = o(o(o(o(test).level1).level2).level3);
var r2 = o(o(o(o(test).level1).level2).foo);
Community
  • 1
  • 1
unitario
  • 6,295
  • 4
  • 30
  • 43
  • 19
    it should be noted that the more % a test has - the SLOWER it is – avalanche1 Feb 03 '17 at 09:12
  • 2
    what about lodash `_.get()` ? how performant is it comparing to those answers? – beniutek May 31 '17 at 14:07
  • 1
    Each method of these is slower or faster than other ones depending on situation. If all keys are found then "Object Wrap" could be fastest, but if one of the keys is not found then "Native solution/Original solution" could be faster. – evilReiko Aug 28 '17 at 06:55
  • 1
    @evilReiko Any method will be slower if no keys are found but in proportion to each other it's still pretty much the same. However, you are right – this is more of an intellectual exercise than anything else. We are talking about a million iterations per millisecond here. I see no use case where it would make much difference. Me personally I would go for `reduce` or `try/catch` out of convenience. – unitario Aug 30 '17 at 06:36
  • How performant is it compared to `try { test.level1.level2.level3 } catch (e) { // some logger e }` – Lex Nov 02 '18 at 02:23
  • for "Object Wrap", if you want strict boolean typing (rather than undefined), you could check the property at the final level: `var r1 = (((test || {}).level1 || {}).level2 || {}).hasOwnProperty('level3');` – dhc Mar 14 '19 at 16:20
  • What about `lodash ` `_.has` method, have you benchmarked it? Maybe it performs better than `_.get`? – pedrosimao Sep 10 '19 at 12:39
46

You can read an object property at any depth, if you handle the name like a string: 't.level1.level2.level3'.

window.t={level1:{level2:{level3: 'level3'}}};

function deeptest(s){
    s= s.split('.')
    var obj= window[s.shift()];
    while(obj && s.length) obj= obj[s.shift()];
    return obj;
}

alert(deeptest('t.level1.level2.level3') || 'Undefined');

It returns undefined if any of the segments is undefined.

kennebec
  • 102,654
  • 32
  • 106
  • 127
  • 3
    Worth noting that this method is very performant, at least in Chrome, in some cases outperforming @Claudiu modified version of selected answer. See performance test here: http://jsperf.com/check-if-deep-property-exists-with-willnotthrow – netpoetica Nov 27 '14 at 05:32
28
var a;

a = {
    b: {
        c: 'd'
    }
};

function isset (fn) {
    var value;
    try {
        value = fn();
    } catch (e) {
        value = undefined;
    } finally {
        return value !== undefined;
    }
};

// ES5
console.log(
    isset(function () { return a.b.c; }),
    isset(function () { return a.b.c.d.e.f; })
);

If you are coding in ES6 environment (or using 6to5) then you can take advantage of the arrow function syntax:

// ES6 using the arrow function
console.log(
    isset(() => a.b.c),
    isset(() => a.b.c.d.e.f)
);

Regarding the performance, there is no performance penalty for using try..catch block if the property is set. There is a performance impact if the property is unset.

Consider simply using _.has:

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

_.has(object, 'a');
// → true

_.has(object, 'a.b.c');
// → true

_.has(object, ['a', 'b', 'c']);
// → true
Gajus
  • 69,002
  • 70
  • 275
  • 438
  • 2
    I think the `try-catch` approach is the best answer. There's a philosophical difference between querying an object for its type, and assuming the API exists and failing accordingly if it doesn't. The latter is more appropriate in loosely typed languages. See http://stackoverflow.com/a/408305/2419669. The `try-catch` approach is also far clearer than `if (foo && foo.bar && foo.bar.baz && foo.bar.baz.qux) { ... }`. – yangmillstheory Jan 31 '16 at 03:39
23

You can also use tc39 optional chaining proposal together with babel 7 - tc39-proposal-optional-chaining

Code would look like this:

  const test = test?.level1?.level2?.level3;
  if (test) alert(test);
Goran.it
  • 5,991
  • 2
  • 23
  • 25
  • Note that this syntax will almost certainly change, as some TC39 members have objections. – jhpratt Sep 24 '18 at 01:59
  • Probably but this will be available in some form in time, and that's the only thing that matters .. It's one of the features I miss the most in JS. – Goran.it Sep 24 '18 at 07:00
  • The syntax did not change and has full support in all major browser latest versions - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#browser_compatibility – jave.web Oct 03 '22 at 15:01
21

how about

try {
   alert(test.level1.level2.level3)
} catch(e) {
 ...whatever

}
user187291
  • 53,363
  • 19
  • 95
  • 127
  • 16
    I don't think try/catch is a good way to test for existence of an object: try/catch is meant to handle exceptions, not normal conditions such as the test here. I think (typeof foo == "undefined") at each step is better -- and in general, there's probably some refactoring required if you're working with such deeply nested properties. Also, try/catch will cause a break in Firebug (and in any browser where break-on-error is turned on) if an exception is thrown. – Sam Dutton Nov 09 '10 at 12:00
  • I vote on this, because browser will check the existence twice if you use other solutions. Lets say you want to call ´a.c.b = 2´. Browser **has to** check the existence before modifying the value (otherwise it would be a memory error caught by OS). –  Sep 02 '13 at 12:04
  • 4
    The question still remain: witch one is faster for browsers to set up a try catch or call `hasOwnProperty()` n times? –  Sep 02 '13 at 12:12
  • 15
    Why is this bad again? This looks cleanest to me. – Austin Pray Jun 04 '14 at 19:26
  • I would say: If you expect that the property exist than it is okay to wrap it into a try block. If it then doesn't exist it is an error. But if you're just lazy and put regular code into the catch block for the case that the property doesn't exist try/catch is misused. Here a if/else or something similar is required. – robsch Aug 09 '16 at 13:55
  • Exceptions for flow control are a [known antipattern](https://softwareengineering.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why). – quickshiftin Mar 15 '21 at 15:41
20

ES6 answer, thoroughly tested :)

const propExists = (obj, path) => {
    return !!path.split('.').reduce((obj, prop) => {
        return obj && obj[prop] ? obj[prop] : undefined;
    }, obj)
}

→see Codepen with full test coverage

Frank N
  • 9,625
  • 4
  • 80
  • 110
  • 1
    I made your tests failed setting the value of the flat prop to 0. You must care about type coercion. – germain Oct 02 '18 at 07:13
  • @germain Does [this](https://codepen.io/fnocke/pen/mzeXoP?editors=0011) work for you? (I explicitly compare `===` for the different falsys, and added test. If you have a better idea, let me know). – Frank N Oct 02 '18 at 11:22
  • I made your tests failed again setting the value of the flat prop to `false`. And then you might want to have a value in your object set to `undefined` (I know it's weird but is is JS). I made a positive false value set to `'Prop not Found'`: `const hasTruthyProp = prop => prop === 'Prop not found' ? false : true const path = obj => path => path.reduce((obj, prop) => { return obj && obj.hasOwnProperty(prop) ? obj[prop] : 'Prop not found' }, obj) const myFunc = compose(hasTruthyProp, path(obj)) ` – germain Oct 02 '18 at 15:19
  • Can you fork my codepen (top-right, easy), correct & add tests, and send me the URL of yours? Thanks =) – Frank N Oct 03 '18 at 18:16
  • Running away to a (huge) 3rd party library... possible, but not my preference. – Frank N Oct 05 '18 at 15:09
  • You can instead use this: `const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x);` credit to [this article](https://medium.com/front-end-hacking/pipe-and-compose-in-javascript-5b04004ac937) – germain Oct 07 '18 at 12:52
  • Rather use `Object.prototype.hasOwnProperty.call(obj, sortKey)` to test if property exists. – sizeight Apr 04 '19 at 09:35
  • 1
    It's irresponsible to leave this unedited after three years. This easily fails on falsy values as @germain pointed out. If this was thoroughly tested, you have some buggy code. – Regular Jo Jun 11 '21 at 03:25
17

This question is old. Today you can use Optional chaining (?.)

let value = test?.level1?.level2?.level3;

Source:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining

Daniel Barral
  • 3,896
  • 2
  • 35
  • 47
11

I tried a recursive approach:

function objHasKeys(obj, keys) {
  var next = keys.shift();
  return obj[next] && (! keys.length || objHasKeys(obj[next], keys));
}

The ! keys.length || kicks out of the recursion so it doesn't run the function with no keys left to test. Tests:

obj = {
  path: {
    to: {
      the: {
        goodKey: "hello"
      }
    }
  }
}

console.log(objHasKeys(obj, ['path', 'to', 'the', 'goodKey'])); // true
console.log(objHasKeys(obj, ['path', 'to', 'the', 'badKey']));  // undefined

I am using it to print a friendly html view of a bunch of objects with unknown key/values, e.g.:

var biosName = objHasKeys(myObj, 'MachineInfo:BiosInfo:Name'.split(':'))
             ? myObj.MachineInfo.BiosInfo.Name
             : 'unknown';
jrode
  • 411
  • 1
  • 4
  • 9
10

I think the following script gives more readable representation.

declare a function:

var o = function(obj) { return obj || {};};

then use it like this:

if (o(o(o(o(test).level1).level2).level3)
{

}

I call it "sad clown technique" because it is using sign o(


EDIT:

here is a version for TypeScript

it gives type checks at compile time (as well as the intellisense if you use a tool like Visual Studio)

export function o<T>(someObject: T, defaultValue: T = {} as T) : T {
    if (typeof someObject === 'undefined' || someObject === null)
        return defaultValue;
    else
        return someObject;
}

the usage is the same:

o(o(o(o(test).level1).level2).level3

but this time intellisense works!

plus, you can set a default value:

o(o(o(o(o(test).level1).level2).level3, "none")
VeganHunter
  • 5,584
  • 2
  • 26
  • 26
  • 2
    `°0o <°(())))><` – Daniel W. Nov 17 '16 at 14:23
  • 2
    I like this one, because it is honest and throws an "undefined" in your face when you don't know your `Object` type. +1. –  Jan 08 '19 at 12:40
  • 2
    As long as you keep the statement in parens you can also call it happy clown technique (o – Sventies Jan 22 '19 at 14:06
  • Thanks Sventies. I love your comment . It is quite nice angle to look from - such conditions are mostly used in "ifs" and always surrounded with external brackets. So, yes, it is mostly a happy clown indeed :))) – VeganHunter Jan 22 '19 at 22:38
  • You really need to be in love with parenthesis to go for this one... – Bastien7 Feb 10 '20 at 09:11
  • @Bastien7 - I used this approach in practice. Parenthesis never presented any issue - modern IDEs make it very easy to match highlighted pairs. You just put a bracket after every field along with dot. It is still better and more compact than other approaches, with very little cost for a lot of convenience. Try it and trust me - it will work better than alternatives. – VeganHunter Feb 10 '20 at 22:59
  • @VeganHunter - Plenty of parentheses makes code harder to read and maintain. Nowadays Typescript proposes safe-navigation operator (like many other up-to-date languages). It is planned in ECMAScript too and is in stage 4: https://github.com/tc39/proposal-optional-chaining – Bastien7 Feb 13 '20 at 11:01
9

create a global function and use in whole project

try this

function isExist(arg){
   try{
      return arg();
   }catch(e){
      return false;
   }
}

let obj={a:5,b:{c:5}};

console.log(isExist(()=>obj.b.c))
console.log(isExist(()=>obj.b.foo))
console.log(isExist(()=>obj.test.foo))

if condition

if(isExist(()=>obj.test.foo)){
   ....
}
kelvin kantaria
  • 1,438
  • 11
  • 15
8

I didn't see any example of someone using Proxies

So I came up with my own. The great thing about it is that you don't have to interpolate strings. You can actually return a chain-able object function and do some magical things with it. You can even call functions and get array indexes to check for deep objects

function resolve(target) {
  var noop = () => {} // We us a noop function so we can call methods also
  return new Proxy(noop, {
    get(noop, key) {
      // return end result if key is _result
      return key === '_result' 
        ? target 
        : resolve( // resolve with target value or undefined
            target === undefined ? undefined : target[key]
          )
    },

    // if we want to test a function then we can do so alos thanks to using noop
    // instead of using target in our proxy
    apply(noop, that, args) {
      return resolve(typeof target === 'function' ? target.apply(that, args) : undefined)
    },
  })
}

// some modified examples from the accepted answer
var test = {level1: {level2:() => ({level3:'level3'})}}
var test1 = {key1: {key2: ['item0']}}

// You need to get _result in the end to get the final result

console.log(resolve(test).level1.level2().level3._result)
console.log(resolve(test).level1.level2().level3.level4.level5._result)
console.log(resolve(test1).key1.key2[0]._result)
console.log(resolve(test1)[0].key._result) // don't exist

The above code works fine for synchronous stuff. But how would you test something that is asynchronous like this ajax call? How do you test that?

fetch('https://httpbin.org/get')
.then(function(response) {
  return response.json()
})
.then(function(json) {
  console.log(json.headers['User-Agent'])
})

sure you could use async/await to get rid of some callbacks. But what if you could do it even more magically? something that looks like this:

fetch('https://httpbin.org/get').json().headers['User-Agent']

You probably wonder where all the promise & .then chains are... this could be blocking for all that you know... but using the same Proxy technique with promise you can actually test deeply nested complex path for it existence without ever writing a single function

function resolve(target) { 
  return new Proxy(() => {}, {
    get(noop, key) {
      return key === 'then' ? target.then.bind(target) : resolve(
        Promise.resolve(target).then(target => {
          if (typeof target[key] === 'function') return target[key].bind(target)
          return target[key]
        })
      )
    },

    apply(noop, that, args) {
      return resolve(target.then(result => {
        return result.apply(that, args)
      }))
    },
  })
}

// this feels very much synchronous but are still non blocking :)
resolve(window) // this will chain a noop function until you call then()
  .fetch('https://httpbin.org/get')
  .json()
  .headers['User-Agent']
  .then(console.log, console.warn) // you get a warning if it doesn't exist
  
// You could use this method also for the first test object
// also, but it would have to call .then() in the end



// Another example
resolve(window)
  .fetch('https://httpbin.org/get?items=4&items=2')
  .json()
  .args
  .items
  // nice that you can map an array item without even having it ready
  .map(n => ~~n * 4) 
  .then(console.log, console.warn) // you get a warning if it doesn't exist
Endless
  • 34,080
  • 13
  • 108
  • 131
  • If someone is interested, I've publish the async version on [npm](https://www.npmjs.com/package/lazy-resolver) – Endless Sep 04 '17 at 12:09
5

Based on this answer, I came up with this generic function using ES2015 which would solve the problem

function validChain( object, ...keys ) {
    return keys.reduce( ( a, b ) => ( a || { } )[ b ], object ) !== undefined;
}

var test = {
  first: {
    second: {
        third: "This is not the key your are looking for"
    }
  }
}

if ( validChain( test, "first", "second", "third" ) ) {
    console.log( test.first.second.third );
}
Community
  • 1
  • 1
Alex Moldovan
  • 2,336
  • 1
  • 16
  • 14
  • 1
    Here is my final approach `function validChain (object, path) { return path.split('.').reduce((a, b) => (a || { })[b], object) !== undefined }` – James Harrington Nov 10 '17 at 21:45
5

I have created a little function to get nested object properties safely.

function getValue(object, path, fallback, fallbackOnFalsy) {
    if (!object || !path) {
        return fallback;
    }

    // Reduces object properties to the deepest property in the path argument.
    return path.split('.').reduce((object, property) => {
       if (object && typeof object !== 'string' && object.hasOwnProperty(property)) {
            // The property is found but it may be falsy.
            // If fallback is active for falsy values, the fallback is returned, otherwise the property value.
            return !object[property] && fallbackOnFalsy ? fallback : object[property];
        } else {
            // Returns the fallback if current chain link does not exist or it does not contain the property.
            return fallback;
        }
    }, object);
}

Or a simpler but slightly unreadable version:

function getValue(o, path, fb, fbFalsy) {
   if(!o || !path) return fb;
   return path.split('.').reduce((o, p) => o && typeof o !== 'string' && o.hasOwnProperty(p) ? !o[p] && fbFalsy ? fb : o[p] : fb, o);
}

Or even shorter but without fallback on falsy flag:

function getValue(o, path, fb) {
   if(!o || !path) return fb;
   return path.split('.').reduce((o, p) => o && typeof o !== 'string' && o.hasOwnProperty(p) ? o[p] : fb, o);
}

I have test with:

const obj = {
    c: {
        a: 2,
        b: {
            c: [1, 2, 3, {a: 15, b: 10}, 15]
        },
        c: undefined,
        d: null
    },
    d: ''
}

And here are some tests:

// null
console.log(getValue(obj, 'c.d', 'fallback'));

// array
console.log(getValue(obj, 'c.b.c', 'fallback'));

// array index 2
console.log(getValue(obj, 'c.b.c.2', 'fallback'));

// no index => fallback
console.log(getValue(obj, 'c.b.c.10', 'fallback'));

To see all the code with documentation and the tests I've tried you can check my github gist: https://gist.github.com/vsambor/3df9ad75ff3de489bbcb7b8c60beebf4#file-javascriptgetnestedvalues-js

V. Sambor
  • 12,361
  • 6
  • 46
  • 65
5

I have used this function for access properties of the deeply nested object and it working for me...

this is the function

/**
 * get property of object
 * @param obj object
 * @param path e.g user.name
 */
getProperty(obj, path, defaultValue = '-') {
  const value = path.split('.').reduce((o, p) => o && o[p], obj);

  return value ? value : defaultValue;
}

this is how I access the deeply nested object property

{{ getProperty(object, 'passengerDetails.data.driverInfo.currentVehicle.vehicleType') }}
lahiru dilshan
  • 690
  • 9
  • 13
5

You can try Optional chaining (but be careful of browser compatibility).

let test = {level1: {level2: {level3: 'level3'}}};

let level3 = test?.level1?.level2?.level3;
console.log(level3); // level3

level3 = test?.level0?.level1?.level2?.level3;
console.log(level3); // undefined

There is a babel plugin(@babel/plugin-proposal-optional-chaining) for optinal chaning. So, please upgrade your babel if necessary.

David
  • 171
  • 2
  • 4
5

One simple way is this:

try {
    alert(test.level1.level2.level3);
} catch(e) {
    alert("undefined");    // this is optional to put any output here
}

The try/catch catches the cases for when any of the higher level objects such as test, test.level1, test.level1.level2 are not defined.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
4

A shorter, ES5 version of @CMS's excellent answer:

// Check the obj has the keys in the order mentioned. Used for checking JSON results.  
var checkObjHasKeys = function(obj, keys) {
  var success = true;
  keys.forEach( function(key) {
    if ( ! obj.hasOwnProperty(key)) {
      success = false;
    }
    obj = obj[key];
  })
  return success;
}

With a similar test:

var test = { level1:{level2:{level3:'result'}}};
utils.checkObjHasKeys(test, ['level1', 'level2', 'level3']); // true
utils.checkObjHasKeys(test, ['level1', 'level2', 'foo']); // false
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • the only issue with this is if there are multiple levels of undefined keys, then you get a TypeError, e.g. `checkObjHasKeys(test, ['level1', 'level2', 'asdf', 'asdf']);` – JKS Jul 19 '12 at 17:34
  • 1
    A more suitable method is *every*, whose value can be returned directly. – RobG Aug 12 '15 at 11:59
  • Maybe change `success = false;` to `return false`. You should bail out once you know it breaks, nothing deeper can exist once it's null or undefined. This would prevent the errors on the deeper nested items, since they obviously don't exist either. – Wade Nov 04 '16 at 20:16
4

I was looking for the value to be returned if the property exists, so I modified the answer by CMS above. Here's what I came up with:

function getNestedProperty(obj, key) {
  // Get property array from key string
  var properties = key.split(".");

  // Iterate through properties, returning undefined if object is null or property doesn't exist
  for (var i = 0; i < properties.length; i++) {
    if (!obj || !obj.hasOwnProperty(properties[i])) {
      return;
    }
    obj = obj[properties[i]];
  }

  // Nested property found, so return the value
  return obj;
}


Usage:

getNestedProperty(test, "level1.level2.level3") // "level3"
getNestedProperty(test, "level1.level2.foo") // undefined
Noah Stahl
  • 6,905
  • 5
  • 25
  • 36
3

The answer given by CMS works fine with the following modification for null checks as well

function checkNested(obj /*, level1, level2, ... levelN*/) 
      {
             var args = Array.prototype.slice.call(arguments),
             obj = args.shift();

            for (var i = 0; i < args.length; i++) 
            {
                if (obj == null || !obj.hasOwnProperty(args[i]) ) 
                {
                    return false;
                }
                obj = obj[args[i]];
            }
            return true;
    }
Anand Sunderraman
  • 7,900
  • 31
  • 90
  • 150
3

Following options were elaborated starting from this answer. Same tree for both :

var o = { a: { b: { c: 1 } } };

Stop searching when undefined

var u = undefined;
o.a ? o.a.b ? o.a.b.c : u : u // 1
o.x ? o.x.y ? o.x.y.z : u : u // undefined
(o = o.a) ? (o = o.b) ? o.c : u : u // 1

Ensure each level one by one

var $ = function (empty) {
    return function (node) {
        return node || empty;
    };
}({});

$($(o.a).b).c // 1
$($(o.x).y).z // undefined
Community
  • 1
  • 1
3

I know this question is old, but I wanted to offer an extension by adding this to all objects. I know people tend to frown on using the Object prototype for extended object functionality, but I don't find anything easier than doing this. Plus, it's now allowed for with the Object.defineProperty method.

Object.defineProperty( Object.prototype, "has", { value: function( needle ) {
    var obj = this;
    var needles = needle.split( "." );
    for( var i = 0; i<needles.length; i++ ) {
        if( !obj.hasOwnProperty(needles[i])) {
            return false;
        }
        obj = obj[needles[i]];
    }
    return true;
}});

Now, in order to test for any property in any object you can simply do:

if( obj.has("some.deep.nested.object.somewhere") )

Here's a jsfiddle to test it out, and in particular it includes some jQuery that breaks if you modify the Object.prototype directly because of the property becoming enumerable. This should work fine with 3rd party libraries.

Brian Sidebotham
  • 1,706
  • 11
  • 15
3

I think this is a slight improvement (becomes a 1-liner):

   alert( test.level1 && test.level1.level2 && test.level1.level2.level3 )

This works because the && operator returns the final operand it evaluated (and it short-circuits).

Julius Musseau
  • 4,037
  • 23
  • 27
3

Here's my take on it - most of these solutions ignore the case of a nested array as in:

    obj = {
        "l1":"something",
        "l2":[{k:0},{k:1}],
        "l3":{
            "subL":"hello"
        }
    }

I may want to check for obj.l2[0].k

With the function below, you can do deeptest('l2[0].k',obj)

The function will return true if the object exists, false otherwise

function deeptest(keyPath, testObj) {
    var obj;

    keyPath = keyPath.split('.')
    var cKey = keyPath.shift();

    function get(pObj, pKey) {
        var bracketStart, bracketEnd, o;

        bracketStart = pKey.indexOf("[");
        if (bracketStart > -1) { //check for nested arrays
            bracketEnd = pKey.indexOf("]");
            var arrIndex = pKey.substr(bracketStart + 1, bracketEnd - bracketStart - 1);
            pKey = pKey.substr(0, bracketStart);
   var n = pObj[pKey];
            o = n? n[arrIndex] : undefined;

        } else {
            o = pObj[pKey];
        }
        return o;
    }

    obj = get(testObj, cKey);
    while (obj && keyPath.length) {
        obj = get(obj, keyPath.shift());
    }
    return typeof(obj) !== 'undefined';
}

var obj = {
    "l1":"level1",
    "arr1":[
        {"k":0},
        {"k":1},
        {"k":2}
    ],
    "sub": {
        "a":"letter A",
        "b":"letter B"
    }
};
console.log("l1: " + deeptest("l1",obj));
console.log("arr1[0]: " + deeptest("arr1[0]",obj));
console.log("arr1[1].k: " + deeptest("arr1[1].k",obj));
console.log("arr1[1].j: " + deeptest("arr1[1].j",obj));
console.log("arr1[3]: " + deeptest("arr1[3]",obj));
console.log("arr2: " + deeptest("arr2",obj));
Seth
  • 10,198
  • 10
  • 45
  • 68
Mike D
  • 101
  • 7
3

This works with all objects and arrays :)

ex:

if( obj._has( "something.['deep']['under'][1][0].item" ) ) {
    //do something
}

this is my improved version of Brian's answer

I used _has as the property name because it can conflict with existing has property (ex: maps)

Object.defineProperty( Object.prototype, "_has", { value: function( needle ) {
var obj = this;
var needles = needle.split( "." );
var needles_full=[];
var needles_square;
for( var i = 0; i<needles.length; i++ ) {
    needles_square = needles[i].split( "[" );
    if(needles_square.length>1){
        for( var j = 0; j<needles_square.length; j++ ) {
            if(needles_square[j].length){
                needles_full.push(needles_square[j]);
            }
        }
    }else{
        needles_full.push(needles[i]);
    }
}
for( var i = 0; i<needles_full.length; i++ ) {
    var res = needles_full[i].match(/^((\d+)|"(.+)"|'(.+)')\]$/);
    if (res != null) {
        for (var j = 0; j < res.length; j++) {
            if (res[j] != undefined) {
                needles_full[i] = res[j];
            }
        }
    }

    if( typeof obj[needles_full[i]]=='undefined') {
        return false;
    }
    obj = obj[needles_full[i]];
}
return true;
}});

Here's the fiddle

adutu
  • 391
  • 3
  • 8
3

Now we can also use reduce to loop through nested keys:

// @params o<object>
// @params path<string> expects 'obj.prop1.prop2.prop3'
// returns: obj[path] value or 'false' if prop doesn't exist

const objPropIfExists = o => path => {
  const levels = path.split('.');
  const res = (levels.length > 0) 
    ? levels.reduce((a, c) => a[c] || 0, o)
    : o[path];
  return (!!res) ? res : false
}

const obj = {
  name: 'Name',
  sys: { country: 'AU' },
  main: { temp: '34', temp_min: '13' },
  visibility: '35%'
}

const exists = objPropIfExists(obj)('main.temp')
const doesntExist = objPropIfExists(obj)('main.temp.foo.bar.baz')

console.log(exists, doesntExist)
Egor Stambakio
  • 17,836
  • 5
  • 33
  • 35
3

You can do this by using the recursive function. This will work even if you don't know all nested Object keys name.

function FetchKeys(obj) {
    let objKeys = [];
    let keyValues = Object.entries(obj);
    for (let i in keyValues) {
        objKeys.push(keyValues[i][0]);
        if (typeof keyValues[i][1] == "object") {
            var keys = FetchKeys(keyValues[i][1])
            objKeys = objKeys.concat(keys);
        }
    }
    return objKeys;
}

let test = { level1: { level2: { level3: "level3" } } };
let keyToCheck = "level2";
let keys = FetchKeys(test); //Will return an array of Keys

if (keys.indexOf(keyToCheck) != -1) {
    //Key Exists logic;
}
else {
    //Key Not Found logic;
}
Ankit Arya
  • 872
  • 7
  • 10
3

And yet another one which is very compact:

function ifSet(object, path) {
  return path.split('.').reduce((obj, part) => obj && obj[part], object)
}

called:

let a = {b:{c:{d:{e:'found!'}}}}
ifSet(a, 'b.c.d.e') == 'found!'
ifSet(a, 'a.a.a.a.a.a') == undefined

It won't perform great since it's splitting a string (but increases readability of the call) and iterates over everything even if it's already obvious that nothing will be found (but increases readability of the function itself).

at least is faster than _.get http://jsben.ch/aAtmc

estani
  • 24,254
  • 2
  • 93
  • 76
2

theres a function here on thecodeabode (safeRead) which will do this in a safe manner... i.e.

safeRead(test, 'level1', 'level2', 'level3');

if any property is null or undefined, an empty string is returned

Ben
  • 1,203
  • 13
  • 8
2

I wrote my own function that takes the desired path, and has a good and bad callback function.

function checkForPathInObject(object, path, callbackGood, callbackBad){
    var pathParts = path.split(".");
    var currentObjectPath = object;

    // Test every step to see if it exists in object
    for(var i=0; i<(pathParts.length); i++){
        var currentPathPart = pathParts[i];
        if(!currentObjectPath.hasOwnProperty(pathParts[i])){
            if(callbackBad){
                callbackBad();
            }
            return false;
        } else {
            currentObjectPath = currentObjectPath[pathParts[i]];
        }
    }

    // call full path in callback
    callbackGood();
}

Usage:

var testObject = {
    level1:{
        level2:{
            level3:{
            }
        }
    }
};


checkForPathInObject(testObject, "level1.level2.level3", function(){alert("good!")}, function(){alert("bad!")}); // good

checkForPathInObject(testObject, "level1.level2.level3.levelNotThere", function(){alert("good!")}, function(){alert("bad!")}); //bad
  • I though fair to give you credit for the inspiration to adapt your code to [my answer](http://stackoverflow.com/a/37601059/2748327) – davewoodhall Jun 02 '16 at 20:02
2
//Just in case is not supported or not included by your framework
//***************************************************
Array.prototype.some = function(fn, thisObj) {
  var scope = thisObj || window;
  for ( var i=0, j=this.length; i < j; ++i ) {
    if ( fn.call(scope, this[i], i, this) ) {
      return true;
    }
  }
  return false;
};
//****************************************************

function isSet (object, string) {
  if (!object) return false;
  var childs = string.split('.');
  if (childs.length > 0 ) {
    return !childs.some(function (item) {
      if (item in object) {
        object = object[item]; 
        return false;
      } else return true;
    });
  } else if (string in object) { 
    return true;
  } else return false;
}

var object = {
  data: {
    item: {
      sub_item: {
        bla: {
          here : {
            iam: true
          }
        }
      }
    }
  }
};

console.log(isSet(object,'data.item')); // true
console.log(isSet(object,'x')); // false
console.log(isSet(object,'data.sub_item')); // false
console.log(isSet(object,'data.item')); // true
console.log(isSet(object,'data.item.sub_item.bla.here.iam')); // true
alejandro
  • 2,799
  • 1
  • 17
  • 25
2

I was having the same issue and and wanted to see if I could come up with a my own solution. This accepts the path you want to check as a string.

function checkPathForTruthy(obj, path) {
  if (/\[[a-zA-Z_]/.test(path)) {
    console.log("Cannot resolve variables in property accessors");
    return false;
  }

  path = path.replace(/\[/g, ".");
  path = path.replace(/]|'|"/g, "");
  path = path.split(".");

  var steps = 0;
  var lastRef = obj;
  var exists = path.every(key => {
    var currentItem = lastRef[path[steps]];
    if (currentItem) {
      lastRef = currentItem;
      steps++;
      return true;
    } else {
      return false;
    }
  });

  return exists;
}

Here is a snippet with some logging and test cases:

console.clear();
var testCases = [
  ["data.Messages[0].Code", true],
  ["data.Messages[1].Code", true],
  ["data.Messages[0]['Code']", true],
  ['data.Messages[0]["Code"]', true],
  ["data[Messages][0]['Code']", false],
  ["data['Messages'][0]['Code']", true]
];
var path = "data.Messages[0].Code";
var obj = {
  data: {
    Messages: [{
      Code: "0"
    }, {
      Code: "1"
    }]
  }
}

function checkPathForTruthy(obj, path) {
  if (/\[[a-zA-Z_]/.test(path)) {
    console.log("Cannot resolve variables in property accessors");
    return false;
  }

  path = path.replace(/\[/g, ".");
  path = path.replace(/]|'|"/g, "");
  path = path.split(".");

  var steps = 0;
  var lastRef = obj;
  var logOutput = [];
  var exists = path.every(key => {
    var currentItem = lastRef[path[steps]];
    if (currentItem) {
      logOutput.push(currentItem);
      lastRef = currentItem;
      steps++;
      return true;
    } else {
      return false;
    }
  });
  console.log(exists, logOutput);
  return exists;
}

testCases.forEach(testCase => {
  if (checkPathForTruthy(obj, testCase[0]) === testCase[1]) {
    console.log("Passed: " + testCase[0]);
  } else {
    console.log("Failed: " + testCase[0] + " expected " + testCase[1]);
  }
});
Jonathan
  • 8,771
  • 4
  • 41
  • 78
2
function getValue(base, strValue) {

    if(base == null) return;
    
    let currentKey = base;
    
    const keys = strValue.split(".");
    
    let parts;
    
    for(let i=1; i < keys.length; i++) {
        parts = keys[i].split("[");
        if(parts == null || parts[0] == null) return;
        let idx;
        if(parts.length > 1) { // if array
            idx = parseInt(parts[1].split("]")[0]);
            currentKey = currentKey[parts[0]][idx];
        } else {
            currentKey = currentKey[parts[0]];
        }
        if(currentKey == null) return;
    }
    return currentKey;
}

Calling the function returns either undefined, if result fails anywhere withing nesting or the value itself

const a = {
  b: {
    c: [
      {
        d: 25
      }
    ]
  }
}
console.log(getValue(a, 'a.b.c[1].d'))
// output
25
Vinit Khandelwal
  • 490
  • 8
  • 20
1

Based on a previous comment, here is another version where the main object could not be defined either:

// Supposing that our property is at first.second.third.property:
var property = (((typeof first !== 'undefined' ? first : {}).second || {}).third || {}).property;
Community
  • 1
  • 1
Juampy NR
  • 2,618
  • 1
  • 25
  • 21
1

Slight edit to this answer to allow nested arrays in the path

var has = function (obj, key) {
    return key.split(".").every(function (x) {
        if (typeof obj != "object" || obj === null || !x in obj)
            return false;
        if (obj.constructor === Array) 
            obj = obj[0];
        obj = obj[x];
        return true;
    });
}

Check linked answer for usages :)

1

I thought I'd add another one that I came up with today. The reason I am proud of this solution is that it avoids nested brackets that are used in many solutions such as Object Wrap (by Oliver Steele):

(in this example I use an underscore as a placeholder variable, but any variable name will work)

//the 'test' object
var test = {level1: {level2: {level3: 'level3'}}};

let _ = test;

if ((_=_.level1) && (_=_.level2) && (_=_.level3)) {

  let level3 = _;
  //do stuff with level3

}

//you could also use 'stacked' if statements. This helps if your object goes very deep. 
//(formatted without nesting or curly braces except the last one)

let _ = test;

if (_=_.level1)
if (_=_.level2)
if (_=_.level3) {

   let level3 = _;
   //do stuff with level3
}


//or you can indent:
if (_=_.level1)
  if (_=_.level2)
    if (_=_.level3) {

      let level3 = _;
      //do stuff with level3
}
kubakoz
  • 34
  • 3
1

Well there are no really good answer for one-liners to use in html templates, so i made one using ES6 Proxies. You just pass an object or value to the "traverse" function and do as much nested calls as you want closing them with function call which will return value or fallback value. Using:

const testObject = { 
  deep: { 
    nested: { 
      obj: { 
        closure: () => { return "closure" },
        number: 9,
        boolean: true,
        array: [1, 2, { foo: { bar: true } }]
      } 
    }
  }
}

traverse(testObject).deep() 
// {nested: {…}}

traverse(testObject).non.existent() 
// undefined

traverse(testObject).deep.nested.obj.closure()() 
// closure

traverse(testObject).deep.nested.obj.array[5]('fallback')
// fallback

traverse(testObject).deep.nested.obj.array[2]()
// {foo: {…}}

traverse(testObject).deep.nested.obj.array[2].foo.bar()
// true

traverse(testObject).deep.nested.obj.array[2].foo.bar[4]('fallback')
// fallback

traverse(testObject).completely.wrong[3].call().WILL_THROW()
// Uncaught TypeError: Cannot read property 'WILL_THROW' of undefined

Function itself:

const traverse = (input) => {
    // unique empty object
    const unset = new Object();
    // we need wrapper to ensure we have access to the same unique empty object
    const closure = (input) => {
        // wrap each input into this
        const handler = new Function();
        handler.input = input;    
        // return wrappers proxy 
        return new Proxy(handler, {
            // keep traversing
            get: (target, name) => {
                // if undefined supplied as initial input
                if (!target.input) {
                    return closure(unset);
                }
                // otherwise
                if (target.input[name] !== undefined) {
                    // input has that property
                    return closure(target.input[name]);
                } else {
                    return closure(unset);
                }
            },
            // result with fallback
            apply: (target, context, args) => {
                return handler.input === unset ? 
                    args[0] : handler.input;
            }
        })
    }
    return closure(input);    
}
pool
  • 11
  • 2
0

Another ES5 solution:

function hasProperties(object, properties) {
    return !properties.some(function(property){
        if (!object.hasOwnProperty(property)) {
            return true;
        }
        object = object[property];
        return false;
    });
}
JKS
  • 3,710
  • 2
  • 29
  • 40
0

My solution that I use since long time (using string unfortunaly, couldn't find better)

function get_if_exist(str){
    try{return eval(str)}
    catch(e){return undefined}
}

// way to use
if(get_if_exist('test.level1.level2.level3')) {
    alert(test.level1.level2.level3);
}

// or simply 
alert(get_if_exist('test.level1.level2.level3'));

edit: this work only if object "test" have global scope/range. else you have to do something like :

// i think it's the most beautiful code I have ever write :p
function get_if_exist(obj){
    return arguments.length==1 || (obj[arguments[1]] && get_if_exist.apply(this,[obj[arguments[1]]].concat([].slice.call(arguments,2))));
}

alert(get_if_exist(test,'level1','level2','level3'));

edit final version to allow 2 method of call :

function get_if_exist(obj){
    var a=arguments, b=a.callee; // replace a.callee by the function name you choose because callee is depreceate, in this case : get_if_exist
    // version 1 calling the version 2
    if(a[1] && ~a[1].indexOf('.')) 
        return b.apply(this,[obj].concat(a[1].split('.')));
    // version 2
    return a.length==1 ? a[0] : (obj[a[1]] && b.apply(this,[obj[a[1]]].concat([].slice.call(a,2))));
}

// method 1
get_if_exist(test,'level1.level2.level3');


// method 2
get_if_exist(test,'level1','level2','level3');
Jack
  • 29
  • 4
0

Another option (close to this answer) :

function resolve(root, path){
    try {
        return (new Function(
            'root', 'return root.' + path + ';'
        ))(root);
    } catch (e) {}
}

var tree = { level1: [{ key: 'value' }] };
resolve(tree, 'level1[0].key'); // "value"
resolve(tree, 'level1[1].key'); // undefined

More on this : https://stackoverflow.com/a/18381564/1636522

Community
  • 1
  • 1
0

Yet another version:

function nestedPropertyExists(obj, props) {
    var prop = props.shift();
    return prop === undefined
        ? true
        : obj.hasOwnProperty(prop) ? nestedPropertyExists(obj[prop], props) : false;
}

nestedPropertyExists({a:{b:{c:1}}}, ['a','b','c']); // returns true
nestedPropertyExists({a:{b:{c:1}}}, ['a','b','c','d']); // returns false
Bartłomiej Szypelow
  • 2,121
  • 15
  • 17
0

I wrote a library called l33teral to help test for nested properties. You can use it like this:

var myObj = {/*...*/};
var hasNestedProperties = leet(myObj).probe('prop1.prop2.prop3');

I do like the ES5/6 solutions here, too.

Nicholas Cloud
  • 1,564
  • 1
  • 13
  • 19
0
function isIn(string, object){
    var arr = string.split(".");
    var notFound = true;
    var length = arr.length;
    for (var i = 0; i < length; i++){
        var key = arr[i];
        if (!object.hasOwnProperty(key)){
            notFound = false;
            break;
        }
        if ((i + length) <= length){
            object = object[key];
        }
    }
    return notFound;
}
var musicCollection = {
    hasslehoff: {
        greatestHits : true
    }
};
console.log(isIn("hasslehoff.greatestHits", musicCollection));
console.log(isIn("hasslehoff.worseHits", musicCollection));

here my String based delimiter version.

JFisher
  • 1
  • 2
0

Based on @Stephane LaFlèche's answer, I came up with my alternative version of the script.

Demo on JSFiddle

var obj = {"a":{"b":{"c":"Hello World"}},"resTest":"potato","success":"This path exists"};
checkForPathInObject = function(object,path,value) {
        var pathParts   = path.split("."),
            result      = false;
        // Check if required parameters are set; if not, return false
        if(!object || typeof object == 'undefined' || !path || typeof path != 'string')
            return false;
        /* Loop through object keys to find a way to the path or check for value
         * If the property does not exist, set result to false
         * If the property is an object, update @object
         * Otherwise, update result */
        for(var i=0;i<pathParts.length;i++){
            var currentPathPart = pathParts[i];
            if(!object.hasOwnProperty( currentPathPart )) {
                result = false;
            } else if (object[ currentPathPart ] && path == pathParts[i]) {
                result = pathParts[i];
                break;
            } else if(typeof object[ currentPathPart ] == 'object') {
                object = object[ currentPathPart ];
            } else {
                result = object[ currentPathPart ];
            }
        }
        /* */
        if(typeof value != 'undefined' && value == result)
            return true;
        return result;
};
// Uncomment the lines below to test the script
// alert( checkForPathInObject(obj,'a.b.c') ); // Results "Hello World"
// alert( checkForPathInObject(obj,'a.success') ); // Returns false
// alert( checkForPathInObject(obj,'resTest', 'potato') ); // Returns true
Community
  • 1
  • 1
davewoodhall
  • 998
  • 3
  • 18
  • 43
0

I am using a function in the following fashion.

var a = {};
a.b = {};
a.b.c = {};
a.b.c.d = "abcdabcd";

function isDefined(objectChainString) {
    try {
        var properties = objectChainString.split('.');
        var currentLevel = properties[0];
        if (currentLevel in window) {
            var consolidatedLevel = window[currentLevel];
            for (var i in properties) {
                if (i == 0) {
                    continue;
                } else {
                    consolidatedLevel = consolidatedLevel[properties[i]];
                }
            }
            if (typeof consolidatedLevel != 'undefined') {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } catch (e) {
        return false;
    }
}

// defined
console.log(checkUndefined("a.b.x.d"));
//undefined
console.log(checkUndefined("a.b.c.x"));
console.log(checkUndefined("a.b.x.d"));
console.log(checkUndefined("x.b.c.d"));
Pratyush
  • 171
  • 1
  • 8
0

The very best and simplest answer is:

var isDefinedPath = function (path) {

    var items = path.split ('.');

    if (!items || items.length < 1 || !(items[0] in window)) { return false; }

    var buffer = [items[0]];
    for (var i = 1, e = items.length; i < e; i ++) {
        buffer.push (items[i]);
        if (eval ('typeof(' + buffer.join ('.') + ') == "undefined"')) {
            return false;
        }
    }

    return true;

}

test: isDefinedPath ('level1.level2.level3');

first level cannot be array, others can

user667540
  • 101
  • 1
  • 3
0

CMS solution works great but usage/syntax can be more convenient. I suggest following

var checkNested = function(obj, structure) {

  var args = structure.split(".");

  for (var i = 0; i < args.length; i++) {
    if (!obj || !obj.hasOwnProperty(args[i])) {
      return false;
    }
    obj = obj[args[i]];
  }
  return true;
};

You can simply use object notation using dot instead of supplying multiple arguments

var test = {level1:{level2:{level3:'level3'}} };

checkNested(test, 'level1.level2.level3'); // true
checkNested(test, 'level1.level2.foo'); // false
zainengineer
  • 13,289
  • 6
  • 38
  • 28
0

Another way to work this out is for example, having the following object :

var x = {
    a: {
        b: 3
    }
};

then, what I did was add the following function to this object :

x.getKey = function(k){
        var r ;
        try {
            r = eval('typeof this.'+k+' !== "undefined"');
        }catch(e){
            r = false;
        }
        if(r !== false){
            return eval('this.'+k);
        }else{
            console.error('Missing key: \''+k+'\'');
            return '';
        }
    };

then you can test :

x.getKey('a.b');

If it's undefined the function returns "" (empty string) else it returns the existing value.

Please also consider this other more complex solution checking the link : JS object has property deep check

Object.prototype.hasOwnNestedProperty = function(propertyPath){
    if(!propertyPath)
        return false;

    var properties = propertyPath.split('.');
    var obj = this;

    for (var i = 0; i < properties.length; i++) {
        var prop = properties[i];

        if(!obj || !obj.hasOwnProperty(prop)){
            return false;
        } else {
            obj = obj[prop];
        }
    }

    return true;
};

// Usage: 
var obj = {
   innerObject:{
       deepObject:{
           value:'Here am I'
       }
   }
}

obj.hasOwnNestedProperty('innerObject.deepObject.value');

P.S.: There is also a recursive version.

Telmo Dias
  • 3,938
  • 2
  • 36
  • 48
0

you can path object and path seprated with "."

function checkPathExist(obj, path) {
  var pathArray =path.split(".")
  for (var i of pathArray) {
    if (Reflect.get(obj, i)) {
      obj = obj[i];
  
    }else{
  return false;
    }
  }
 return true;
}

var test = {level1:{level2:{level3:'level3'}} };

console.log('level1.level2.level3 => ',checkPathExist(test, 'level1.level2.level3')); // true
console.log( 'level1.level2.foo => ',checkPathExist(test, 'level1.level2.foo')); // false
0

Here's a little helper function I use that, to me, is pretty simple and straightforward. Hopefully it's helpful to some :).

static issetFromIndices(param, indices, throwException = false) {
    var temp = param;

    try {
        if (!param) {
            throw "Parameter is null.";
        }

        if(!Array.isArray(indices)) {
            throw "Indices parameter must be an array.";
        }

        for (var i = 0; i < indices.length; i++) {
            var index = indices[i];
            if (typeof temp[index] === "undefined") {
                throw "'" + index + "' index is undefined.";
            }


            temp = temp[index];
        }
    } catch (e) {
        if (throwException) {
            throw new Error(e);
        } else {
            return false;
        }
    }

    return temp;
}

var person = {
    hobbies: {
        guitar: {
            type: "electric"
        }
    }
};

var indices = ["hobbies", "guitar", "type"];
var throwException = true;

try {
    var hobbyGuitarType = issetFromIndices(person, indices, throwException);
    console.log("Yay, found index: " + hobbyGuitarType);
} catch(e) {
    console.log(e);
}
Schmeedty
  • 83
  • 1
  • 5
  • It will be more useful if you could add some detail about your answer, like how this code is going to fix the problem and what it does? – DarkSuniuM Feb 21 '19 at 02:51
0
getValue (o, key1, key2, key3, key4, key5) {
    try {
      return o[key1][key2][key3][key4][key5]
    } catch (e) {
      return null
    }
}
0

There's a little pattern for this, but can get overwhelming at some times. I suggest you use it for two or three nested at a time.

if (!(foo.bar || {}).weep) return;
// Return if there isn't a 'foo.bar' or 'foo.bar.weep'.

As I maybe forgot to mention, you could also extend this further. Below example shows a check for nested foo.bar.weep.woop or it would return if none are available.

if (!((foo.bar || {}).weep || {}).woop) return;
// So, return if there isn't a 'foo.bar', 'foo.bar.weep', or 'foo.bar.weep.woop'.
// More than this would be overwhelming.
Smally
  • 1,556
  • 1
  • 8
  • 27
0

If you happen to be using AngularJs you can use the $parse service to check if a deep object property exists, like this:

if( $parse('model.data.items')(vm) ) {
    vm.model.data.items.push('whatever');
}

to avoid statements like this:

if(vm.model && vm.model.data && vm.model.data.items) {
    ....
}

don't forget to inject the $parse service into your controller

for more info: https://glebbahmutov.com/blog/angularjs-parse-hacks/

Michiel
  • 4,160
  • 3
  • 30
  • 42
0

Quite a lot of answers but still: why not simpler?

An es5 version of getting the value would be:

function value(obj, keys) {
    if (obj === undefined) return obj;
    if (keys.length === 1 && obj.hasOwnProperty(keys[0])) return obj[keys[0]];
    return value(obj[keys.shift()], keys);
}

if (value(test, ['level1', 'level2', 'level3'])) {
  // do something
}

you could also use it with value(config, ['applet', i, 'height']) || 42

Credits to CMS for his ES6 solution that gave me this idea.

iRaS
  • 1,958
  • 1
  • 16
  • 29
0
function propsExists(arg) {
  try {
    const result = arg()
  
    if (typeof result !== 'undefined') {
      return true
    }

    return false
  } catch (e) {
    return false;
  }
}

This function will also test for 0, null. If they are present it will also return true.

Example:

function propsExists(arg) {
  try {
    const result = arg()
  
    if (typeof result !== 'undefined') {
      return true
    }

    return false
  } catch (e) {
    return false;
  }
}

let obj = {
  test: {
    a: null,
    b: 0,
    c: undefined,
    d: 4,
    e: 'Hey',
    f: () => {},
    g: 5.4,
    h: false,
    i: true,
    j: {},
    k: [],
    l: {
        a: 1,
    }
  }
};


console.log('obj.test.a', propsExists(() => obj.test.a))
console.log('obj.test.b', propsExists(() => obj.test.b))
console.log('obj.test.c', propsExists(() => obj.test.c))
console.log('obj.test.d', propsExists(() => obj.test.d))
console.log('obj.test.e', propsExists(() => obj.test.e))
console.log('obj.test.f', propsExists(() => obj.test.f))
console.log('obj.test.g', propsExists(() => obj.test.g))
console.log('obj.test.h', propsExists(() => obj.test.h))
console.log('obj.test.i', propsExists(() => obj.test.i))
console.log('obj.test.j', propsExists(() => obj.test.j))
console.log('obj.test.k', propsExists(() => obj.test.k))
console.log('obj.test.l', propsExists(() => obj.test.l))
Walter Monecke
  • 2,386
  • 1
  • 19
  • 52
0

Simply use https://www.npmjs.com/package/js-aid package for checking for the nested object.

0

How about this function? Instead of needing to list each nested property separately, it maintains the 'dot' syntax (albeit in a string) making it more readable. It returns undefined or the specified default value if the property isn't found, or the value of the property if found.

val(obj, element, default_value)
    // Recursively checks whether a property of an object exists. Supports multiple-level nested properties separated with '.' characters.
    // obj = the object to test
    // element = (string or array) the name of the element to test for.  To test for a multi-level nested property, separate properties with '.' characters or pass as array)
    // default_value = optional default value to return if the item is not found. Returns undefined if no default_value is specified.
    // Returns the element if it exists, or undefined or optional default_value if not found.
    // Examples: val(obj1, 'prop1.subprop1.subsubprop2');
    // val(obj2, 'p.r.o.p', 'default_value');
    {

        // If no element is being requested, return obj. (ends recursion - exists)
        if (!element || element.length == 0) { return obj; }

        // if the element isn't an object, then it can't have properties. (ends recursion - does not exist)
        if (typeof obj != 'object') { return default_value; }

        // Convert element to array.
        if (typeof element == 'string') { element = element.split('.') };   // Split on dot (.)

        // Recurse into the list of nested properties:
        let first = element.shift();
        return val(obj[first], element, default_value);

    }
Ryan Griggs
  • 2,457
  • 2
  • 35
  • 58
-1
/**
 * @method getValue
 * @description simplifies checking for existance and getting a deeply nested value within a ceratin context
 * @argument {string} s       string representation of the full path to the requested property 
 * @argument {object} context optional - the context to check defaults to window
 * @returns the value if valid and set, returns undefined if invalid / not available etc.
 */
var getValue = function( s, context ){
    var fn = function(){
        try{
            return eval(s);
        }catch(e){
            return undefined;
        }
    }
    return fn.call(context||window,s);
}

and usage :

if( getValue('a[0].b[0].b[0].d') == 2 ) // true
-1

Another way :

/**
 * This API will return particular object value from JSON Object hierarchy.
 *
 * @param jsonData : json type : JSON data from which we want to get particular object
 * @param objHierarchy : string type : Hierarchical representation of object we want to get,
 *                       For example, 'jsonData.Envelope.Body["return"].patient' OR 'jsonData.Envelope.return.patient'
 *                       Minimal Requirements : 'X.Y' required.
 * @returns evaluated value of objHierarchy from jsonData passed.
 */
function evalJSONData(jsonData, objHierarchy){
    
    if(!jsonData || !objHierarchy){
        return null;
    }
    
    if(objHierarchy.indexOf('["return"]') !== -1){
        objHierarchy = objHierarchy.replace('["return"]','.return');
    }
    
    let objArray = objHierarchy.split(".");
    if(objArray.length === 2){
        return jsonData[objArray[1]];
    }
    return evalJSONData(jsonData[objArray[1]], objHierarchy.substring(objHierarchy.indexOf(".")+1));
}
Ravi Jiyani
  • 919
  • 1
  • 10
  • 26
-2

I automated the process

if(isset(object,["prop1","prop2"])){
// YES!

}

function isset(object, props){
    var dump;
    try {
        for(var x in props){
            if(x == 0) {
                dump = object[props[x]];
                return;
            }
            dump = dump[props[x]];
        }
    } catch(e) {
        return false;
    }

    return true;
}
The Orca
  • 1,250
  • 2
  • 17
  • 31
  • A couple things to note. You're doing a `for/in` over an array, which is not recommended. That is really meant for objects. There's no guarantee that the order of execution will be consistent. Also, if you're going to loop over the properties, I probably wouldn't use `try/catch`. I think you'll get better performance taking a `if( props[x] in object )` approach, or `if( object.hasOwnProperty(props[x]) )` if you don't want to include `prototype` properties. My situation was such that I was only interested in the deepest property. That's why I chose `try/catch`. – user113716 Oct 27 '10 at 14:55
  • If you look closely, I move in the object level using the dump variable, I am not staying at level 1 – The Orca Oct 28 '10 at 13:01
  • but your right about for/in, my heart is broken :/, it is also slower – The Orca Oct 28 '10 at 13:11
-4

Just wrote this function today which does a deep search for a property in a nested object and returns the value at the property if found.

/**
 * Performs a deep search looking for the existence of a property in a 
 * nested object. Supports namespaced search: Passing a string with
 * a parent sub-object where the property key may exist speeds up
 * search, for instance: Say you have a nested object and you know for 
 * certain the property/literal you're looking for is within a certain
 * sub-object, you can speed the search up by passing "level2Obj.targetProp"
 * @param {object} obj Object to search
 * @param {object} key Key to search for
 * @return {*} Returns the value (if any) located at the key
 */
var getPropByKey = function( obj, key ) {
    var ret = false, ns = key.split("."),
        args = arguments,
        alen = args.length;

    // Search starting with provided namespace
    if ( ns.length > 1 ) {
        obj = (libName).getPropByKey( obj, ns[0] );
        key = ns[1];
    }

    // Look for a property in the object
    if ( key in obj ) {
        return obj[key];
    } else {
        for ( var o in obj ) {
            if ( (libName).isPlainObject( obj[o] ) ) {
                ret = (libName).getPropByKey( obj[o], key );
                if ( ret === 0 || ret === undefined || ret ) {
                    return ret;
                }
            }
        }
    }

    return false;
}
Xaxis
  • 1,935
  • 15
  • 17
  • i think question is about verifying the existence of property not search it out in any one of the children of the object which is different. `a.b.c` and `a.e.c` will both return value if `urfunc(a,'c')` is called. function like `if(exists(a.b.c))` is ideal – Muhammad Umer Feb 24 '15 at 17:23
-4

In typeScript you can do something like this:

 if (object.prop1 && object.prop1.prop2 && object.prop1.prop2.prop3) {
    const items = object.prop1.prop2.prop3
    console.log(items);
 }
Abs0lem
  • 1
  • 1