For anybody who still wants a solution for this :
function getStringOutOfHierarchy(obj){
let finalString=[];
let lastString=[];
let stringHierarachy = recHierarchy(obj, Object.keys(obj))[0];
finalString.map(elem=>{
if(elem.constructor===Array){
elem.forEach(
(subelem=>{
lastString.push(subelem);
})
);
}
});
return lastString;
function recHierarchy(obj, initial){
if(Object.keys(obj).length!=0){
return Object.keys(obj).map((elem)=>{
let basString= elem;
switch (typeof obj[elem]) {
case 'string':{
return [`${elem.toString()}`];
}
default :{
return [`${elem.toString()}`];
}
case 'object':{
if(obj[elem].constructor === Array ){
return [`${elem.toString()}`];
}
let returnedValue = recHierarchy(obj[elem]);
let value = returnedValue.map(
(elem)=>{
if(elem.constructor === Array){
return elem.map(
(subelem)=>{
return `${basString}.${subelem.toString()}`;
}
);
}
return `${basString}.${elem.toString()}`;
}
);
(initial && initial.indexOf(elem)!==-1)?finalString=finalString.concat(value) :null;
return value;
}
}
});
}else{
return [''];
}
}
}
the above code will return a list of strings that represent the full hierarchy of the obj given in the argument :
Example :
input object :
{
itemOne:{
subItemOneOne:{},
subItemOneTwo:{
SubSubItemOneTwoOne:'ZEZEZEZEZEZEZE',
SubSubItemOneTwoTwo:"kkokokokok"
},
subItemOneThree:['erererer', 'ffdfdfdf']
},
itemTwo:{
subItemTwoOne:["popopopo","éopopopo"]
}
}
output array of hierarchies :
[ 'itemOne.subItemOneOne.',
'itemOne.subItemOneTwo.SubSubItemOneTwoOne',
'itemOne.subItemOneTwo.SubSubItemOneTwoTwo',
'itemOne.subItemOneThree',
'itemTwo.subItemTwoOne' ]
Note : a small modification can be done to add the possibility of extracting arrays as 0 and 1 keys thus having itemOne.subItemOneThree.0 and itemOne.subItemOneThree.1 as separate hierarchical strings.