Skip to content

Instantly share code, notes, and snippets.

@SergioR82
Created November 17, 2018 22:50
Show Gist options
  • Save SergioR82/c0c99e85f997b623eb34899c4cfd0192 to your computer and use it in GitHub Desktop.
Save SergioR82/c0c99e85f997b623eb34899c4cfd0192 to your computer and use it in GitHub Desktop.
Traverse books with TCO implementation
function TraverseWithTOC([obj,...subarray], resultArray = [], currentObj = {}){
if (obj === undefined) return resultArray;
if (typeof obj === 'object' && !(isObjectEmpty(obj))){
let hasPropArray = false;
let resultObj = {};
for (let property in obj) {
if (obj.hasOwnProperty(property)) {
if (isPrimitive(obj[property]))
resultObj[property] = obj[property];
else
if (Array.isArray(obj[property])){
hasPropArray = true;
TraverseWithTOC(obj[property], resultArray, resultObj);
};
};
};
if (!hasPropArray)
resultArray.push(Object.assign({},currentObj,resultObj));
};
return TraverseWithTOC(subarray,resultArray, currentObj);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment