Thursday, 26 December 2019

Enumerable Range in Javascript

The following Javascript code uses Generator functions and destructuring spread operator ... to create an array between min and count, much similar to Enumerable.Range in Linq and C#. The following screen shot shows how it can be used. You can iterate this with for.. let.. of

function* Range(min,ct) {
  for (let x=min;x<=min+ct;x++) {
    yield x;
  }
}

let gen = [...Range(2,10)]
console.log(gen);

for (let y of gen) {
 console.log(y);
}


Share this article on LinkedIn.

No comments:

Post a Comment