Skip to content

Instantly share code, notes, and snippets.

@iandesj
Last active November 14, 2019 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iandesj/3db31aa6fa93811a941b7189add81cec to your computer and use it in GitHub Desktop.
Save iandesj/3db31aa6fa93811a941b7189add81cec to your computer and use it in GitHub Desktop.
Examples of Array.from() vs for loop
const year = 2019;
const month = 10; // november, zero-indexed
const startDay = 14;
console.log('Array.from() example');
const dateList = Array.from({length:7}, (val, idx) => {
return new Date(year, month, startDay + idx);
});
console.log('dateList = ', dateList, '\n');
console.log('for loop example');
const dateListAgain = []
for (let i=0; i < 7; i++) {
dateListAgain.push(new Date(year, month, startDay + i));
}
console.log('dateListAgain = ', dateListAgain);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment