How to check if an object structure exists?

Suppose I parse a JSON object from a third-party source:

var myObject = { person_list: [ { black_hair: { list: [ 'bob', 'john', 'allen' ]} } ] }; 

But if the structure suddenly changes or perhaps the data corruption was damaged, how can I check for the presence of the deepest parts of the structure?

I can do

 if ( myObject.person_list.black_hair.list !== undefined ) { // do stuff } 

But perhaps black_hair does not exist in some cases. If it is not in the object, I get Uncaught TypeError: Cannot read property 'list' of undefined . So the only way I can check if the whole structure is complete is to check if each level is defined:

 if ( myObject.person_list !== undefined ) { if ( myObject.person_list.black_hair !== undefined ) { if ( myObject.person_list.black_hair.list !== undefined ) { // do stuff } } } 

But this is a little ridiculous. Is there an easy way to handle this in JavaScript? Try it, get the best approach?

+6
source share
3 answers

You can define a function to check the complete structure for you:

 function defined_structure(obj, attrs) { var tmp = obj; for(i=0; i<attrs.length; ++i) { if(tmp[attrs[i]] == undefined) return false; tmp = tmp[attrs[i]]; } return true; } //... if(defined_structure(myObject, ['person_list', 0, 'black_hair', 'list']) { // Do stuff } 

The first parameter is an object with the structure to be checked, and the second is an array with the name of the attached properties.

Update:

As pointed out by @chiliNUT, person_list is an array. Anyway, this approach works by adding the index of the item you want to check (ie ['person_list', 0, 'black_hair', 'list'] ).

+2
source

you can use this function that I wrote to check if a property is set. You just need to pass the property path as a string.

 // Check if nested object properties exist on unlimited levels // param: str 'obj.property.property' function isset (property) { // split path to object property var properties = property.split('.'); var obj = this; //loop through each portion of the path checking if it exists for (var i = 0; i < properties.length; i++) { var current_property = properties[i]; var next_property = i < properties.length - 1 ? true : false; // IF current property exists then we need to check the next level if (obj[current_property] !== null && typeof obj[current_property] === 'object' && next_property) { obj = obj[current_property]; continue; } return obj.hasOwnProperty(current_property); } } if ( isset('myObject.person_list.black_hair.list')) { // do stuff } 
0
source

slightly better with a meta object that displays the expected format

 var meta = { name: "person_list", type: [], component: [{ name: 'black_hair', type: {}, component: { name: 'list', type: [] } }] }; var myObject = { person_list: [{ asdfa: { list: [ 'bob', 'john', 'allen'] } }] }; function defined(meta, obj) { if (meta.name == 'list' && obj[meta.name] && obj[meta.name].length) { return true; } if (!obj[meta.name]) { return false; }; if (Object.prototype.toString.call(meta.type) === '[object Array]' && !obj[meta.name].length) { return false; } else if (Object.prototype.toString.call(meta.type) === '[object Array]' && obj[meta.name].length) { for (index in obj[meta.name]) { return defined(meta.component[index], obj[meta.name][index]); } } else if (Object.prototype.toString.call(meta.type) === '[object Object]') { return defined(meta.component, obj[meta.name]); } } console.log(defined(meta, myObject)); 

Demo

0
source

Source: https://habr.com/ru/post/972744/


All Articles