Reputation: 322572
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.
Upvotes: 880
Views: 418001
Reputation: 261
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.
Upvotes: 8
Reputation: 37895
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
Upvotes: 8
Reputation: 4096
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
Upvotes: 20
Reputation: 2758
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);
}
Upvotes: 0
Reputation: 627
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
Upvotes: 2
Reputation: 367
Simply use https://www.npmjs.com/package/js-aid package for checking for the nested object.
Upvotes: 0
Reputation: 959
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));
}
Upvotes: 0
Reputation: 938
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') }}
Upvotes: 5
Reputation: 2558
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))
Upvotes: 0
Reputation: 827852
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.
Upvotes: 734
Reputation: 1448
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)){
....
}
Upvotes: 9
Reputation: 2136
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.
Upvotes: 0
Reputation:
/**
* @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
Upvotes: -1
Reputation: 26527
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
Upvotes: 3
Reputation: 4260
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/
Upvotes: 0
Reputation: 1684
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.
Upvotes: 0
Reputation:
getValue (o, key1, key2, key3, key4, key5) {
try {
return o[key1][key2][key3][key4][key5]
} catch (e) {
return null
}
}
Upvotes: 0
Reputation: 1
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);
}
Upvotes: -4
Reputation: 93
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);
}
Upvotes: 0
Reputation: 35
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
Upvotes: 0
Reputation: 11
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);
}
Upvotes: 1
Reputation: 13409
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
Upvotes: 6
Reputation: 6299
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);
Upvotes: 23
Reputation: 10416
const propExists = (obj, path) => {
return !!path.split('.').reduce((obj, prop) => {
return obj && obj[prop] ? obj[prop] : undefined;
}, obj)
}
Upvotes: 20
Reputation: 900
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;
}
Upvotes: 3
Reputation: 4178
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.
Upvotes: 0
Reputation: 6535
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);
Upvotes: 238
Reputation: 34
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
}
Upvotes: 1
Reputation: 13889
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
Upvotes: 0
Reputation: 11
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 :)
Upvotes: 1