Few neat JS snippets
Som Shekhar Mukherjee
Posted on November 9, 2020
Creating a 2D Array initialized with zeroes
const nrows = 5;
const ncols = 5;
const arr = Array.from({ length: nrows }, () =>
Array.from({ length: ncols }, () => 0)
);
Flattening a 2D array into a 1D array
const oneD = [].concat(...twoDArr);
Array.from()
can be really handy at times
const primes = [2, 3, 5, 7, 11];
const sqPrimes = Array.from(primes, (x) => x * x);
💖 💪 🙅 🚩
Som Shekhar Mukherjee
Posted on November 9, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
javascript How I Created Vanilla Calendar Pro — A Lightweight and Flexible JavaScript Calendar with TypeScript
November 28, 2024