const test = {
a: { id: 1 },
b: { id: 2 },
c: [{ a: 1 }, { a: 2 }, { c: 3 }]
};
function* generLoop(elem) {
// eslint-disable-next-line no-undef,no-restricted-syntax
for (el of test[elem]) {
// eslint-disable-next-line no-undef
yield el;
}
}
function createLoop(elem) {
let gen;
if (elem === 'c') {
gen = generLoop(elem);
}
return () => (gen ? gen.next() : test[elem]);
}
const iterator = createLoop('c');
for (let i = 0; i < 3; i++) {
console.log(iterator());
}