nested loop array js code example
Example 1: iterate through nested object javascript
function nestedLoop(obj) {
const res = {};
function recurse(obj, current) {
for (const key in obj) {
let value = obj[key];
if(value != undefined) {
if (value && typeof value === 'object') {
recurse(value, key);
} else {
res[key] = value;
}
}
}
}
recurse(obj);
return res;
}
Example 2: javascript nested loop
for (let exercise = 1; exercise <= 3; exercise++) {
console.log(`---------- Starting exercise ${exercise}`)
for (let rep = 1; rep <= 6; rep++) {
console.log(`Exercise ${exercise}: Lifting weight repetitition ${rep}`);
}
}