I want to check if a deeply nested object consists of single key-value pair, if it does, it should return true else false.
For e.g., I want the below code to return true,
var test = {
level1:
{
level2:
{
level3:'level3'
}
}
};
And the below code to return false,
var test2 = {
level1: {
level2: {'a': '1', 'b': 2},
level3: {'a': '2', 'c': 4}
}
};
Also, the below code should return true,
var test3 =
{
level1: {
level2: {
level3: {
level4: {
1: "1",
2: "2",
3: "3",
4: "4"
}
}
}
}
}
I made the following program for it, but it doesn't work,
function checkNested(obj) {
if(typeof(obj) === 'object') {
if(Object.keys(obj).length === 1) {
var key = Object.keys(obj);
checkNested(obj[key])
} else {
return(false);
}
}
return(true);
}
Could someone please suggest me how can I achieve it?