When developing my React Native app, I encountered an issue with the navigation function. In developer mode, all arguments passed to the function are outputted to the console. However, when a large store is sent as an argument, it cannot be printed due to a cyclic object reference error caused by the deep structure of the object. To address this, I decided to create a function that will inspect all fields of the object and only output information to the console if the field is deeper than one level.
const notDeepObj = {
name: 'John',
surname: 'Robert',
age: 28,
family: false,
};
const deepObj = {
name: 'John',
surname: 'Robert',
bankAccount: {
accounts: 2,
cash: true,
credit false,
wasCreated: {
city: 'New-York',
date: '12.02.2020.',
}
}
}
function checkDepthOfObject(obj){}
If the object is not deep, the function should return the object itself:
checkDepthOfObject(notDeepObj)
//it will return:
{
name: 'John',
surname: 'Robert',
age: 28,
family: false,
};
For a deep object, the function should return non-deep fields along with a flag indicating the presence of deep fields:
checkDepthOfObject(notDeepObj)
//it will return:
{
name: 'John',
surname: 'Robert',
bankAccount: '[DEEP_OBJECT]'
};
I would appreciate any recommendations on the best approach to accomplish this task.