Skip to content

Instantly share code, notes, and snippets.

@EliasSalom
Last active March 15, 2022 01:01
Show Gist options
  • Save EliasSalom/e6661caae71b81f6749f960ed62303db to your computer and use it in GitHub Desktop.
Save EliasSalom/e6661caae71b81f6749f960ed62303db to your computer and use it in GitHub Desktop.
ObjectLoop
function LoopOverObject(obj)
{
for (let prop in obj) {
console.log(prop, obj[prop]);
// Check if Arry of (numbers or strings)
if(typeof obj[prop] === 'object' && Array.isArray(obj[prop]) === true)
{
for(let arr of obj[prop])
{
console.log(">> "+arr);
}
}
// Check if object
else if(typeof obj[prop] === 'object' && Array.isArray(obj[prop]) === false)
{
console.log(obj[prop]);
LoopOverObject(obj[prop])
}
// print the rest of strings, numbers, boolean
else console.log(">>> "+obj[prop]);
// else if (typeof prop === String || typeof prop === Number || typeof prop ===Boolean)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment