Online educator and mentor for Sr. JS developers
With 20+ years of dev experience, I'm an online educator, mentor, and consultant--teaching practical, advanced topics to developers like you on how to write: ✅ new AI/LLM features ✅ maintainable JS code ✅ scalable web apps ...yup that's about it! 🎤
Markdown supported · ⌘↵ to post
One of my favourite util functions using Array.from:
const listRange = (begin, end, inc = 1) =>
Array.from({ length: Math.ceil((end - begin) / inc) }, (_, i) => begin + i * inc);
// console.log(listRange(0, 4, 1)); // [0, 1, 2, 3]
// console.log(listRange(0, 4)); // [0, 1, 2, 3]
// console.log(listRange(1, 10, 2)); // [1, 3, 5, 7, 9]
Steven Olsen
Bonus tip: how to turn your POO (plain old object) into an iteratable array!
const obj1 = {a: 1, b: 2}; obj1.__proto__[Symbol.iterator] = function* () { for (const k in this) yield [k, this[k]]; } for (const e of obj1) console.log(e.join(":")) // a:1 // b:2